how to give legend according to excel data headers
3 次查看(过去 30 天)
显示 更早的评论
i am writing a gui code to plot the excel data having 352 rows and 4columns.Likewise i have multiple excel data sheets,which i have to change according to userspecification,so i had written ad code which allows me to choose any excel files and plot the graph acoordingly . now my problem is to give legend ,every time whenever ishould choose the file with the change of plots the legend should also change and they should be named after the excel headers .below is the code which i have written
% 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)
[filename, pathname] = uigetfile({'*.xlsx'});
values = xlsread(strcat(pathname,filename));
x=values(:,1);
a=values(:,2);
b=values(:,3);
c=values(:,4);
hold on
plot(x,a,'r')
plot(x,b,'g')
plot(x,c,'b')
legend;
hold off
handles.xvalues=x;
handles.yvalues=a;
handles.yvalues=b;
handles.yvalues=c;
i do not know how to code legend according to excel headers
0 个评论
回答(1 个)
Voss
2022-4-23
One way to get the headers out of the .xlsx file:
[values,str_values] = xlsread('test.xlsx')
How to apply that in your code:
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)
[filename, pathname] = uigetfile({'*.xlsx'});
% values = xlsread(strcat(pathname,filename));
[values,str_values] = xlsread(strcat(pathname,filename));
x=values(:,1);
a=values(:,2);
b=values(:,3);
c=values(:,4);
hold on
plot(x,a,'r')
plot(x,b,'g')
plot(x,c,'b')
% legend();
legend(str_values(2:4));
hold off
handles.xvalues=x;
handles.yvalues=a;
handles.yvalues=b;
handles.yvalues=c;
guidata(hObject,handles); % <-- presumably you want to save the updated handles structure
end
You might have to do different or additional things, depending on the exact contents of your xlsx files.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!