Plot graph based on exist uitable data

1 visualización (últimos 30 días)
Anewa
Anewa el 10 de En. de 2021
Respondida: Deepak el 5 de Sept. de 2024
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);

Respuestas (1)

Deepak
Deepak el 5 de Sept. de 2024
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.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by