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

조회 수: 2 (최근 30일)
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

채택된 답변

Adam Danz
Adam Danz 2020년 4월 28일
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
sydney salvador
sydney salvador 2020년 4월 28일
편집: sydney salvador 2020년 4월 28일
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 2020년 4월 28일
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));

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by