how to Make a button upload a .txt file and another button to read the file and plot it in matlab app design?

1 visualización (últimos 30 días)
Do you have any tips in making my buttons work, i did the code in normal matlab and it works but i dont know how to translate it in app designer
  1. the code in calculate button should plot a line graph showing the peaks
This is for my upload button, it should keep the file "data.txt"
% Button pushed function: UploaddataButton
function UploaddataButtonPushed(app, event)
[file,path] = uigetfile('*.txt')
end
This is for my "calculate" button, the code should read the uploaded file and plot the graph
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
fid=fopen('Data.txt');
s=textscan(fid,'%f %f','headerlines',2);
g = cell2mat(s);
x=g(:,1);
y=g(:,2);
[sortedX, sortIndex] = sort(x);
sortedY = y(sortIndex);
sortedXLimit = sortedX(1:160,:);
sortedYLimit = sortedY(1:160,:);
findpeaks(sortedYLimit,sortedXLimit,'MinPeakProminence',205,'Annotate','extents')
[psor,lsor] = findpeaks(sortedYLimit,sortedXLimit,'MinPeakProminence',205,'SortStr','descend')
set(gca, 'XDir','reverse')
text(lsor+.02,psor,num2str((1:numel(psor))'))
end

Respuesta aceptada

Adam Danz
Adam Danz el 28 de Abr. de 2020
When you select the file with uigetfile, the outputs need to be stored as a property of the App so all callback functions have access to the files selected.
Here's instructions how to set the file and path variables as app properties and here's a demo you can follow.
In your CalculateButtonPushed function, after the file and path variables have been declared as properties, you can access the user's selection:
fid=fopen(fullfile(app.Path, app.File));
% ^^^^^^^^ ^^^^^^^^^ replace with your var names
  3 comentarios
sydney salvador
sydney salvador el 28 de Abr. de 2020
Editada: sydney salvador el 28 de Abr. de 2020
Hello thank you I have no problem with the properties but can I ask you again with the "fid=fopen" i dont understand understand what i should put in "app.File" my code below says there is an error, i have placed a "plot" command for UI axes in the last part to show the graph of data
properties (Access = public)
selectpath='';
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: UploadButton
function UploadButtonPushed(app, event)
[file,path] = uigetfile('*.txt')
end
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
fid=fopen(Data.txt(app.selectpath));
fclose(fid); %Error using textscan
% Invalid file identifier.
% Use fopen to generate a valid file identifier.
seek=textscan(fid,'%f %f','headerlines',2);
g = cell2mat(seek);
x=g(:,1);
y=g(:,2);
[sortedX, sortIndex] = sort(x);
sortedY = y(sortIndex);
sortedXLimit = sortedX(1:160,:);
sortedYLimit = sortedY(1:160,:);
plot(app.UIAxes,value*peaks) %for the graph
app.UIAxes.YLim = [0 205];
Adam Danz
Adam Danz el 28 de Abr. de 2020
My understanding is that this function,
function UploaddataButtonPushed(app, event)
[app.file, app.path] = uigetfile('*.txt')
end
allows the user to select a file and the outputs are stored as properties of the app. The that file is read by,
fid=fopen(fullfile(app.Path, app.File));

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by