You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB中如何追加表格行持续记录数据?多程序数据合并求助

Hey there! Let's break down your two MATLAB table questions step by step—starting with the basics of appending rows, then fixing that frustrating single-row issue with your robot and sensor data.

问题1:如何在MATLAB中持续追加表格行记录数据?

There are a few straightforward, reliable ways to append rows to a table for ongoing data logging:

  • Direct bracket indexing (simplest for small datasets):If your new data matches the table's column structure (either as a small table or a row vector), just use table = [table; newRow] to tack it on. Example:

    % Initialize your base table
    dataLog = table([1;2], {'A';'B'}, 'VariableNames', {'SampleID', 'ReadingLabel'});
    
    % Create a new row (as a table)
    newEntry = table(3, {'C'}, 'VariableNames', {'SampleID', 'ReadingLabel'});
    
    % Append the new row
    dataLog = [dataLog; newEntry];
    

    If your new data is a raw array, convert it to a table first with array2table:

    rawNewData = [4, 'D'];
    dataLog = [dataLog; array2table(rawNewData, 'VariableNames', {'SampleID', 'ReadingLabel'})];
    
  • Use vertcat (explicit row concatenation):This works exactly like the bracket method but makes your intent clearer:

    dataLog = vertcat(dataLog, newEntry);
    
  • Preallocate for large datasets (performance boost):If you know roughly how many rows you'll log upfront, preallocate the table's size first to avoid repeated memory reallocation (way faster for big datasets):

    totalExpectedRows = 1000;
    % Initialize empty table with specified column types and size
    dataLog = table(zeros(totalExpectedRows,1), strings(totalExpectedRows,1), ...
        'VariableNames', {'SampleID', 'ReadingLabel'});
    
    % Fill rows in a loop
    for i = 1:totalExpectedRows
        dataLog(i,:) = table(i, string(char(64+i)), 'VariableNames', {'SampleID', 'ReadingLabel'});
    end
    

问题2:合并机器人与传感器数据仅得到一行的解决方案

From your code snippet, the most likely culprit is that you're not accumulating data across loop iterations—either overwriting the table each time or initializing it inside the loop. Let's fix that:

First, confirm your function behavior

Make sure Thesis_calibration() (your robot data function) and your sensor data function (let's call it readSensorData() for this example) each return a single row of table data with consistent column names every time they're called.

Fix the main program structure

Move your total table initialization outside the while loop so it retains data across iterations. Here's a corrected code example:

% 1. Initialize the combined table FIRST (outside the loop!)
% Grab initial data to set up the table structure
initialRobotData = Thesis_calibration();
initialSensorData = readSensorData();

% Rename columns if there's overlap (to avoid merge errors)
initialSensorData.Properties.VariableNames = strcat('Sensor_', initialSensorData.Properties.VariableNames);

% Combine initial rows into your master table
combinedData = [initialRobotData, initialSensorData];
% Optional: Track timestamps separately (or just use the timestamp column in the table)
Time_Stamp = [initialRobotData.TimeStamp]; % Assuming your robot data has a TimeStamp column

% 2. Run the loop to collect and append data
while true
    % Get fresh rows of robot and sensor data
    newRobotRow = Thesis_calibration();
    newSensorRow = readSensorData();
    % Rename sensor columns again to match the master table
    newSensorRow.Properties.VariableNames = strcat('Sensor_', newSensorRow.Properties.VariableNames);
    
    % Merge the new rows and append to the master table
    newCombinedRow = [newRobotRow, newSensorRow];
    combinedData = [combinedData; newCombinedRow];
    
    % Update timestamp array if needed
    Time_Stamp = [Time_Stamp; newRobotRow.TimeStamp];
    
    % Add an exit condition (critical—otherwise the loop runs forever!)
    if size(combinedData, 1) >= 500 || ~ishandle(gcf) % Stop after 500 rows or if a figure is closed
        break;
    end
    
    % Add a small delay to avoid overwhelming your hardware
    pause(0.1);
end

Key troubleshooting checks

  • Did you initialize the table inside the loop? If yes, every iteration resets it to empty, leaving only the last row. Move the master table initialization outside the loop—this is the #1 mistake here.
  • Column name conflicts? If robot and sensor tables share column names, MATLAB will throw an error. Rename one set of columns (like prefixing sensor columns with Sensor_) as shown above.
  • Are your functions returning arrays instead of tables? Convert raw arrays to tables with array2table before merging, and specify column names to match your master table.

内容的提问来源于stack exchange,提问作者Deyire Yusuf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:28:51