Matlab:无法处理数据

时间:2016-07-28 09:23:25

标签: matlab matrix

我正在Matlab编写一个程序,读取来自传感器的Matrix(微控制器是Arduino Leonardo)。您可以选择记录矩阵的速度(例如,每分钟60帧将是每秒一个矩阵)。

所有这一切都很好,但我必须创建一个默认文件,记录所有不会被记录的矩阵(假设它足够快,每秒记录8个矩阵,你只需要每秒一个矩阵,然后是默认文件会记录其他七个)。否则我会被赶出这里:

%Get data
%Data is read as string (CSV)
data = str2double(strsplit(fgetl(serialPort), ','));

if and(get(hObject,'Value') == 1, correctPort == 1)
    %try for right ports
    try
        %Reshape data (1D -> 2D array) and display in matrix
        k = 1;
        for i = 1:nrow
            for j = 1:ncol
                % Reading in tag names
                tagNames{k} = ['tag_matrix_' num2str(k)];
                % Creating matrix
                data2d(row_index(i), col_index(j)) = data(k);
                % Display in matrix
                set(handles.(tagNames{k}),'String',data2d(i,j));
                set(handles.(tagNames{k}),'visible','on');
                k = k + 1;
            end
        end
    catch
        set(handles.tag_txt3,'String','This is not the correct port. Please look up the correct port in your Device Manager. Set up the right port and start again');
        set(handles.tag_OnOff,'Value',0);
        set(handles.tag_log,'Value',0);
        set(handles.tag_log,'Enable','off')
        correctPort = 0;
    end
end

它经历了几次循环(每次不同次数)但最终我会被发送到“catch”。如果我开始录制,就会发生这种情况。没关系。

如果我在命令窗口中显示矩阵(data2d(row_index(i), col_index(j)) = data(k)没有;),它就会起作用。

以下是我记录数据的方式:

% Open for writing
% Preparation
if and(startLog == 1, correctPort == 1)
    set(handles.tag_txt3,'String','Program running and recording data');
end
% Set interval = recTime after logging (don't worry about that)
if and(startLog == 1, counter == 0)
    interval = recTime;
    counter = 1;
end
% Making sure you have to restart after stop logging
if and(startLog == 0, counter == 1)
    set(handles.tag_OnOff,'Value',0)
end
% Record data when the time has come
if and(startLog == 1,time > interval)
    fprintf(fid, '#');
    fprintf(fid, '%u', matrixNumber);
    fprintf(fid, '\n');
    for i=1:size(data2d,1)
        fprintf(fid, '%g\t', data2d(i,:));
        fprintf(fid,'\n');
    end
    interval = recTime + interval;
    matrixNumber = matrixNumber + 1;
    %Making sure you have choosen a unit and send out error
elseif and(startLog == 1,recTime == 0)
    set(handles.tag_txt3,'String','Choose a unit and start again');
    set(handles.tag_OnOff,'Value',0)
    set(handles.tag_log,'Value',0)
    counter = 0;
else
    %This is where the default file would be recording all the other matrices
end

希望有人可以帮助我。我不想放入整个代码,因为它已经有十页了。如果您还有其他问题,请与我们联系。

非常感谢你提前

麦克

0 个答案:

没有答案
相关问题