Plot graph based on exist uitable data

1 回表示 (過去 30 日間)
Anewa
Anewa 2021 年 1 月 10 日
回答済み: Deepak 2024 年 9 月 5 日
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 件のコメント
fitriyanni jeff
fitriyanni jeff 2021 年 1 月 10 日
i also have the same problem

サインインしてコメントする。

回答 (1 件)

Deepak
Deepak 2024 年 9 月 5 日
Hi @Anewa,
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 containNaN” 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.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by