MATLAB Answers

shnrndll
0

Why is my assigned variable not being detected/read?

Asked by shnrndll
on 27 Jun 2019
Latest activity Commented on by Jan
on 2 Jul 2019
Accepted Answer by Jan
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 Comments

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.
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.
@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.

Sign in to comment.

1 Answer

Answer by Jan
on 28 Jun 2019
Edited by Jan
on 28 Jun 2019
 Accepted Answer

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 Comments

Show 1 older 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.
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.

Sign in to comment.