Error using textscan Invalid file identifier Use fopen to generate a valid file identifier in app designer

I use a button to upload my "Data.txt" file and another button to calculate and plot it into UIaxes but im having error in "fid=fopen"
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];

5 Comments

I was trying to fix the porblem and saw that "fid" always highlighted but it always shows "Unrecognized method, property, or field 'path' for class 'testapp1'."
I only understood that my upload button has stored the file as properties and my calculate button should get the file from properties and calculate it.

Sign in to comment.

 Accepted Answer

It doesn't seem like your upload button stores anything. It saves the file and path into file and path and then exits, at which point the file and path are lost. This would work:
function UploadButtonPushed(app, event)
[file,path] = uigetfile('*.txt');
if file == 0
% handle case where user clicks cancel
else
app.selectpath = fullfile(path, file);
end
end
And then in your other callback:
function CalculateButtonPushed(app, event)
fid=fopen(app.selectpath);
seek=textscan(fid,'%f %f','headerlines',2);
...

10 Comments

plot(app.UIAxes,value*peaks)
app.UIAxes.YLim = [0 205];
I have UI.axes placed in app designer and would like to know where to put this code to plot inside the axes
in my current code it is saying 'error'
function CalculateButtonPushed(app, event)
fid=fopen(Data.txt(app.selectpath));
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];
If this is your function, neither value nor peaks is defined before your call to plot. What should they equal? MATLAB doesn't have a clue.
I assumed that i can replace "value" with "x" and "peaks" with "y" but an error occured
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'
I also used sortedXLimit & sortedYLimit but same error, my apologies for slow understanding I am self learning this for a year
No worries! What exactly are you trying to plot? MATLAB interprets an asterisk * by itself as matrix multiplication, and matrix multipication A*B is only valid if A has the same number of columns as B has rows. Assuming x and y are both column vectors (with dimension Nx1 each), then x*y isn't valid as long as N != 1. If you want to multiply each element within x by the corresponding element within y, you can use element-wise multiplication, or x.*y. Note the period before the asterisk.
my data is compiled in a ".txt" into 2 columns with nth rows i am trying to make the button "calculate" to process the data and plot it into Ui.axes the code within the button worked in normal matlab my problem is plotting it in app design
Hmm... I'll assume you want to plot your sorted and trimmed y over your sorted and trimmed x, in which case you should try
plot(app.UIAxes,sortedXLimit,sortedYLimit)
If that's not what you want, could you provide the code which worked for you?

Sign in to comment.

More Answers (1)

[sortedX, sortIndex] = sort(x);
sortedY = y(sortIndex);
sortedXLimit = sortedX(1:160,:);
sortedYLimit = sortedY(1:160,:);
plot(app.UIAxes,sortedXLimit,sortedYLimit)
app.UIAxes.YLim = [0 1000];
hold on
findpeaks(sortedYLimit,sortedXLimit,'MinPeakProminence',205,'Annotate','extents')
sir my peaks arent showing on the graph on app designer, it shows in another window in matlab and when i use hold on it does not work

Categories

Community Treasure Hunt

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

Start Hunting!