How can I plot the first 2 columns from a .csv file when the user selects an option from a pop menu

조회 수: 4 (최근 30일)
This is the code I have, what is the next step to callback the 'option 1' at the pop menu? and that it plots the 2 first colums?
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%
handles.fileName=uigetfile({'*.csv;';'*.mat';'*.*'},...
'File Selector');
C = strsplit(handles.fileName,'.');

채택된 답변

Maadhav Akula
Maadhav Akula 2019년 8월 12일
Use the readmatrix function rather than strsplit. This may help you :-
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.fileName=uigetfile({'*.csv;';'*.mat';'*.*'},...
'File Selector');
guidata(hObject, handles);%This updates the handles structure
Now at the popupmenu1_Callback: -
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
contents = cellstr(get(hObject,'String'));%Gets the cell string of popupmenu
pop_choice = contents{get(hObject,'Value')};
if(strcmp(pop_choice,'option 1'))%Checking for option 1
array = readmatrix(handles.fileName);%Reading the csv file
col1 = array(:, 1);%Values of Column 1
col2 = array(:, 2);%Values of Column 2
plot(col1,col2);%Plotting Column 1 vs Column 2
end
You can refer the following link to know more about readmatrix: -
Hope this helps!
  댓글 수: 1
Gemma Malagón
Gemma Malagón 2019년 8월 12일
Yes, thank you very mkuch. What I want to do is after I select one option of the pop menu (10 options in total) and then I press the load button to load a .csv file a graph plots the first 2 columns if I select the 1st option of the menu. Then if I select option 2 I want it to plot columns 9 and 10, then fro option 3 I want it to plot columns 17 and 18, and so on for the rest of the options of the popmenu.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by