Plot graph based on exist uitable data
1 次查看(过去 30 天)
显示 更早的评论
My GUI has a editable uitable. I set it 20x2. But based on user data, the table is not full. My slope and intercept code only run when the table is full. If not it disp NaN. How can i code it based on only existing data on uitable?
Data = str2double(get(handles.TableField,'Data'))
t = Data(:,1)
Raw = Data(:,2)
C = log(raw)
plot(t,C)
P=polyfit(t,C,1)
slope = P(1)
set(handles.text8,'String',slope);
guidata(hObject, handles);
intercept = P(2)
set(handles.text14,'String',intercept);
回答(1 个)
Deepak
2024-9-5
I understand that you have created an editable UI Table with dimensions 20x2, and you want to fill the table based on user data. However, the user data has missing values, and your code works only when the user data is complete, so that the table gets filled completely.
To handle the cases where UI Table is not fully populated, we can first filter out the rows that contain “NaN” values, before performing the calculations. To do that, we can use logical indexing in MATLAB. Below expression returns a logical array indicating rows without “NaN” values.
rows = ~any(isnan(Data), 2);
Please find attached the documentation of “isnan” function in MATLAB for reference:
Here is the complete MATLAB code that addresses the task:
Data = get(handles.TableField, 'Data');
Data = str2double(Data);
% Remove rows with NaN values
Data = Data(~any(isnan(Data), 2), :);
% Check if there is enough data to perform the calculation
if size(Data, 1) >= 2
t = Data(:, 1);
raw = Data(:, 2);
C = log(raw);
plot(t, C);
P = polyfit(t, C, 1);
slope = P(1);
intercept = P(2);
% Update the GUI with slope and intercept
set(handles.text8, 'String', slope);
set(handles.text14, 'String', intercept);
else
% Handle the case where there is not enough data
set(handles.text8, 'String', 'Insufficient data');
set(handles.text14, 'String', 'Insufficient data');
end
% Update the handles structure
guidata(hObject, handles);
I anticipate this will resolve the issue.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!