3. Display variables of mat file, and Plot variables on the axes
After loading a mat file in structure type, click a mat file, and then variables in the mat file selected will appear on the two pop-up menus of X and Y.
With one or two variables selected, we can draw a plot on the axes.
To implement these functions, one is to know each variable saved in a mat file, and the other is to draw a plot with variables selected.
1) Know each variable in a mat file
After clicking a mat file on the listbox, variables in a mat file in the structure type displays on the two pop-up handles.
Create a callback function of the listbox as below.
------------------------------------------------------------------------
function FileList_Callback(hObject, eventdata, handles)
% hObject handle to FileList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns FileList contents as cell array
% contents{get(hObject,'Value')} returns selected item from FileList
try
index = get(handles.FileList,'Value');
file = get(handles.FileList,'String');
filename = file(index); filename = filename{1,1};
filenamePath = fullfile(handles.directory,filename);
catch
% ME
%warndlg(ME.message);
return;
end
% check a file extension
comma = strfind(filenamePath,'.');
extension = filenamePath(comma(1)+1:end);
switch extension
case 'mat'
result = load(filenamePath); % load structure mat file
VariableList = fieldnames(result); % extract field names
handles.result = struct2cell(result);
% Display variables on the pop-up of Y
set(handles.VariablesList,'String',VariableList);
% Display variables on the pop-up of X
set(handles.xVariables,'String',VariableList);
handles.legend = filename;
set(handles.selectedFile,'String',filename);
guidata(hObject,handles);
-------------------------------------------------------------------------
2) Draw a plot
% --- Executes on button press in postprocessing
function plot_Callback(hObject, eventdata, handles)
% hObject handle to postprocessing_ui (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
warning('off');
index = get(handles.VariablesList,'Value');
holdaxis = get(handles.holdon,'Value');
checkp = get(handles.xPlotCheck,'Value'); % x-axis on/off check
% is the plot handles field defined for the handles structure?
if ~isfield(handles,'pline');
handles.pline = [];
% handles.pline will be used afterward.
end
try
% create a legend name
tleg = handles.legend(1:end-4);
yvar = get(handles.VariablesList,'String');
yvari = get(handles.VariablesList,'Value'); ytemp = yvar(yvari);
leg = sprintf('%s%s%s',tleg,': ',ytemp{1});
result = handles.result;
y = result{index,1};
% Hold on
if isequal(holdaxis,1)
hold(handles.axes1,'all');
if isequal(checkp,1)
% with x-axis variable selected
xn = get(handles.xVariables,'String');
xi = get(handles.xVariables,'Value'); xn = xn{xi,1};
x = result{xi,1};
pline = plot(handles.axes1,x,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
xlabel(xn,'interpreter','none');
ylabel(ytemp{1},'interpreter','none');
else
% default
pline = plot(handles.axes1,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
ylabel(ytemp{1},'interpreter','none');
end
set(hl,'Interpreter','none');
% Hold off
else
% Initialization of handles.pline
handles.pline = [];
hold(handles.axes1,'off');
if isequal(checkp,1)
% with x-axis variable selected
xn = get(handles.xVariables,'String');
xi = get(handles.xVariables,'Value'); xn = xn{xi,1};
x = result{xi,1};
pline = plot(handles.axes1,x,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
xlabel(xn,'interpreter','none');
ylabel(ytemp{1},'interpreter','none');
else
% default
pline = plot(handles.axes1,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
ylabel(ytemp{1},'interpreter','none');
end
set(hl,'Interpreter','none');
end
% update the plot handles to the array of plot handles
handles.pline = [handles.pline ; pline];
guidata(hObject,handles);
grid(handles.axes1,'on');
% Save the plot legend
handles.hl = hl;
guidata(hObject,handles);
catch ME
warndlg(ME.message,'No data files');
guidata(hObject,handles);
%warndlg('Please first load data files','No data files');
end
Related posts
0. Build simple plot tool.
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Display variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)