Why is my assigned variable not being detected/read?

This is where the problem starts, when I feed the user selected file(app.FileSelected) into this function, the selected file string should be read and then identified with any of the cases shown above. Based on the respective case I expect an output of a N dimensional cell array read out into the command window. But instead it is saying that variable UserData is not assigned. But as you can see UserData is initially equated to the reading of the binary file and then based on the string case it is reshaped.....PLEASE HELP SEE E

3 commentaires

Jan
Jan le 28 Juin 2019
The question would be easier to read and to answer, if you post the code and the error message as text, not as screenshots.
By the way, do not use "path" as name of a variable, because this is an important Matlab function. During debugging this can have strange side effects.
Adam
Adam le 2 Juil 2019
As per your flag, we don't delete questions that have answers and further comments that people have put time and effort into as there is potentially still something useful for others in those answers too.
You can edit your question to clarify it better if you wish, though leave the substance of it as is because the answers need to still make sense relative to the question.
Jan
Jan le 2 Juil 2019
@shnrndll: After somebody has posted an answer, we avoid removing a question, because then the time for answering would be wasted. Readers learn from questions and answers and it is the nature of questions, that they contain errors and unclear details.
So please leave the thread untouched (I see, the code is removed already...) and open a new one, if this helps you.

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 28 Juin 2019
Modifié(e) : Jan le 28 Juin 2019
Your code creates app.UserData, but the error message tells you, that the output UserData is not created:
function UserData = ReadChkptFile(app, FileSelected, File_ID)
% ^^^^^^^^ ^^ ? ^^ ?
In addition the 2nd and 3rd input argument are marked in the editor, which means, that they are not used anywhere. You call this function like this:
% line 134
app.UserData = ReadChkptFiole(app, app.FileSelected, app.File_ID);
But most likely this would be sufficient already:
ReadChkptFiole(app);
because the variables for input and output are contained in the app already.
By the way, the expression fullfile(file) is exactly the same as file , so simply omit the fullfile() in the definition of app.FileSelected.

4 commentaires

shnrndll's "Answer" moved here:
Wow, please see code in text form below.Thank you so much for your quick response that small adjustment definitely helped. No longer getting the error code but unfortunately Im not getting my desired output either. Please see output below :
The output should be a cell array of intergers shaped according to the matching case. And it looks as if the first function OpenFileDirectory is functioning correctly, hence the file path shown in the command window. But function,
UserData = ReadChkptFile(app, FileSelected, File_ID) isnt.....
Am i defining UserData correctly?? Why arent the switch/case statements being read??
properties (Access = public)
%Declare the properties of the File Inspector
FileSelected
File_Path
File_ID
UserData
Data
end
methods (Access = public)
%File Selection
function OpenFileDirectory(app)
[file, path] = uigetfile('*.*','Select Checkpoint File');%Current file selection
filename = fullfile(path, file);
app.File_Path = filename;
app.FileSelected = file;
if isequal(app.File_Path, 0)
disp('User Selected Cancel');
else
disp(fullfile(app.File_Path));
app.File_ID = fopen(app.File_Path,'r','b');
end
end
function UserData=ReadChkptFile(app,FileSelected,File_ID)
app.UserData=fread(app.File_ID,inf,'float32');
if nargin > 1
switch app.FileSelected
case N.....%%shown in initial comment
Again you post code as a screenshot. Please post it as text, because then I can use it by copy&paste to post an asnwer. Currently I have to retype your code again.
You cann the function by:
ReadChkptFile(app)
without an output argument. In the definition of the function:
UserData=ReadChkptFile(app,FileSelected,File_ID)
there is an output argument UserData. This variable is not defined anywhere, but just app.UserData.
"Am i defining UserData correctly?" - No, this variable is not defined at all.
"hy arent the switch/case statements being read?" - Why do you assume, that they are not read? Simply set a breakpoint in the code to see what it does.
Stephen23
Stephen23 le 1 Juil 2019
Modifié(e) : Stephen23 le 1 Juil 2019
You wrote the swtich inside an if , whose condition depends on the number of input arguments:
function ReadChkptFile(app, FileSelected, File_ID)
...
if nargin > 1
switch app.FileSelected
...
end
...
end
The switch never runs because you call that function once with one input argument:
ReadChkptFile(app);
Also note that 'A.*' is not a valid filename on Windows (and even if the other case conditions are valid filenames, without extensions they are unlikely to be the names of data files returned by uigetfile).
@shnrndll: Could you please stop posting code as screenshots? It is really hard to read them and using the contain information by copy&paste to post an answer is not working. Do not make it hard to type answers.
Of course Matlab stops at the breakpoint: this is the purpose of break-points. You have to step through the code line by line then. Please learn how to use the debugger: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
In the screenshot you can see, that the 2nd and 3rd input argument is marked. They have an orange background, to mention, that these variables are not used inside the function. Then omit them to decrease the clutter.
"my code is saving the user selected file {app.FileSelected} as an character array" - Of course: app.FileSelected is the name of the file. Storing this as char vector is the best idea.
Note: Using the curly braces in the text is confusing, because they mean a cell array in Matlab. Use the standard method to mark code by marking the term with the mouse and press the "M" in the title bar. So prefer app.FileSelected instead of writing {app.FileSelected}.
switch app.FileSelected
case 'A.*'
This compares the file name with the char vector 'A.*'. File names cannot contain a star under Windows (is this allowed under Linux?). Therefore it is not clear what you expect the code to do. A meaningful comment is recommended.
This command:
app.UserData = fread(app.File_ID,inf,'float32');
is performed for all cases. The call it once before the switch.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Question posée :

le 27 Juin 2019

Commenté :

Jan
le 2 Juil 2019

Community Treasure Hunt

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

Start Hunting!

Translated by