Try catch error 'Unrecognized function or variable'
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I have the following code in response to a button push:
function ButtonInvoicePushed(app, event)
try
tmpInvoiceForm = SalesInvoiceForm;
catch
tmpInvoiceForm.delete
end
end
SalesInvoiceForm is a class that opens a GUI.
With this code, MATLAB is giving me the warning that
'The value assigned to variable tmpInvoiceForm might be unused'.
When the button is pressed, I also get the error:
Unrecognized function or variable 'tmpInvoiceForm'.
tmpInvoiceForm.delete
tmpInvoiceForm does have a delete method for error handling.
I feel like I'm using try/catch wrongly, can someone please explain? Thanks
0 commentaires
Réponses (2)
Rik
le 23 Jan 2023
The code in the try block will be executed normally. If any line results in an error, the execution will abort, and the catch block will execute.
Your problem is that the only way the catch block is executed, is if there is an error in the line assigning a value to tmpInvoiceForm. That means the variable is actually not assigned any value, so calling a delete method on it is not possible.
This code looks like it is inside the GUI, so you should be able to do this:
function ButtonInvoicePushed(app, event)
try
tmpInvoiceForm = SalesInvoiceForm;
catch
app.delete
end
end
Otherwise, you could consider an edit like this (which requires editing SalesInvoiceForm).
function ButtonInvoicePushed(app, event)
try
fig = uifigure;
tmpInvoiceForm = SalesInvoiceForm('parent',fig);
catch
delete(fig)
end
end
2 commentaires
Rik
le 29 Jan 2023
You have one line of code in your try block. If there is an error, it must be in that one line. If that line results in an error, the variable will not be assigned.
So if you then try to use that non-existent variable in your catch block, you get a new error.
Image Analyst
le 23 Jan 2023
The warning is that tmpInvoiceForm is never used, like it said. You assigned it, but so what? You then never use it in any other computations. Remember all variables in a function are local and vanish once that function exits. So unless you do something to capture the variable for the rest of your program, the rest of your program will never see that variable. Perhaps you want to make it a global variable like
app.tmpInvoiceForm = tmpInvoiceForm;
just before the final end statement of the function.
The error is that in the first line of the program you try to do
tmpInvoiceForm = SalesInvoiceForm;
but SalesInvoiceForm was never assigned in that function so it does not exist yet. So that throws and error and you go into the catch which tries to do
tmpInvoiceForm.delete
however since tmpInvoiceForm was never assigned anything, there is no delete() method and it will throw another error.
Perhaps you assigned it in another function but never made it global, which you can do by assigning it to a field of app:
app.SalesInvoiceForm = SalesInvoiceForm;
then, in this function when you go to try to use it you should do
app.tmpInvoiceForm = app.SalesInvoiceForm;
2 commentaires
Image Analyst
le 29 Jan 2023
So you're saying that SalesInvoiceForm is like another GUI that the user interacts with and returns some object/class variable that should have a "delete" method? OK, but the answer is still what I said above. You accept tmpInvoiceForm but never do anything with it, so that's the warning. If you don't want to use it, then just do
SalesInvoiceForm;
by itself and don't accept any return value from it. That will avoid the warning about the return value never being used.
Then is seems that, for some reason (not for the warning reason though) an error is thrown and you get tossed into the catch block. I don't know what that original error is (you didn't say) but now it tries to execute the catch block where you say
tmpInvoiceForm.delete;
well I don't even know if tmpInvoiceForm exists and the error message seems to say it does NOT exist, probably because the call to SalesInvoiceForm threw an error internally. It's possible the error bubbled up to the calling routine which is why you ended up on that line in the catch block of ButtonInvoicePushed in the calling routine.
If you have any more questions, then attach your data and code (.m or .mlapp files) with the paperclip icon after you read this:
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!