How can I load and plot a signal with GUI MATLAB?

5 visualizaciones (últimos 30 días)
Mohammad Shahbakhti
Mohammad Shahbakhti el 26 de Abr. de 2019
Comentada: Mohammad Shahbakhti el 27 de Abr. de 2019
Hi all,
I want to load a signal ('.mat') from my pc and when I push the Plot button, plot it. I wrote the following codes but I have the errors
Error using plot
Invalid first data argument.
Error in readfile>pushbutton2_Callback (line 95)
plot(handles.signal)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in readfile (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)readfile('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback"
Here's the code.
function varargout = readfile(varargin)
% READFILE MATLAB code for readfile.fig
% READFILE, by itself, creates a new READFILE or raises the existing
% singleton*.
%
% H = READFILE returns the handle to a new READFILE or the handle to
% the existing singleton*.
%
% READFILE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in READFILE.M with the given input arguments.
%
% READFILE('Property','Value',...) creates a new READFILE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before readfile_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to readfile_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help readfile
% Last Modified by GUIDE v2.5 25-Apr-2019 10:20:12
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @readfile_OpeningFcn, ...
'gui_OutputFcn', @readfile_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before readfile is made visible.
function readfile_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to readfile (see VARARGIN)
% Choose default command line output for readfile
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes readfile wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = readfile_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- 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)
[filename pathname]=uigetfile({'*.mat'}, 'File Browser');
fullpathname=strcat(pathname, filename);
signal=fileread(fullpathname);
set(handles.text3,'String',fullpathname);
handles.signal = signal;
guidata(hObject, handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1)
plot(handles.signal)
I was wondering if you could help me. Tnx
  6 comentarios
Mohammad Shahbakhti
Mohammad Shahbakhti el 26 de Abr. de 2019
I am a bit confused now!! So what commmands should I use to load the data? with my code, there is Browser botton which you can load the data from PC. The problem is about plotting error. I was wondering if you could help me to solve this problem. Tnx
Adam
Adam el 26 de Abr. de 2019
You can use the debugger to check that your data has been loaded as expected first, and that it is available to you in the Pushbutton2 callback. Then the plot error should become obvious.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 26 de Abr. de 2019
datastruct = load(fullpathname);
%we will assume that the data is in the first variable in the
% .mat file, no matter what it is named.
fn = fieldnames(datastruct);
signal = datastruct.(fn{1})

Categorías

Más información sobre Just for fun 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