using external matlab file variables in app desighner
Afficher commentaires plus anciens
Hello , I have written matlab code in *.M matlab file.
this matlab code has many variables and expressions.
i want to use this code in the Matlab file in my APP(i am new to matlab apps)
is there some example i could use to undesrtand this issue?
Thanks.
5 commentaires
fima v
le 29 Jan 2022
dpb
le 29 Jan 2022
There really is no "issue" in using external code in an app -- the same rules of finding functions hold there as elsewhere. This is demonstrated in that all MATLAB functions are available transparently in the app; you don't have to do anything special to use them.
You may want to create a special folder/subdirectory for this and other personal functions you may want to use in this app, or if this is a general m-file that you will want to use widely, then put it in a location that you then add to the MATLABPATH to make it visible.
The alternative would be to make a copy of it as an internal file within the application itself; this would keep it as a local function and hidden from the outside world; the downside might be that if it is a general application function then you would have two versions to maintain.
The choice on where you choose to put it is more to do with whether it is only associated with this app and was just developed independently first or whether you can envision using it elsewhere.
Either way, get it into a location on the MATLAB search path and it will be visible like any other.
fima v
le 29 Jan 2022
Réponses (2)
function UpdateButtonPushed(app, event)
app.UpdateButton.Enable='off'; app.UpdateButton.Text="Working"; app.UpdateButton.FontColor='r';
app.QuitButton.Enable='off';
drawnow nocallbacks
if ~isfile(app.billQualFile)
h=errordlg([app.billQualFile "Not Found. Must Set Billing File First."],'modal');
waitfor(h)
app.UpdateButton.Text="Update"; app.UpdateButton.FontColor='k'; app.UpdateButton.Enable='on';
app.QuitButton.Enable='on';
drawnow
return
end
sheets=sheetnames(app.billQualFile);
% ... bunch more error-checking code elided as superfluous...
% probably 100 lines of code, none of which is anything more than more of the same
% of the above if block for all the possible things that could happen...
% insert a progress dialog for grins...
h=uiprogressdlg(app.RestrictedFundsAwardsWorkbookUpdateToolUIFigure, ...
"Title",'Please Standby...',"Message",'Reading Billing',"Indeterminate","on");
% and finally, do the work...
[tBill,tPay]=readBilling(app.billQualFile,app.billSheet,app.billUpdate);
h.Message='Writing Awards';
writeAwards(tBill,tPay,app.year,app.semester,app.awardQualFile,app.awardSheet);
h.Message='Update Complete';
pause(0.5)
close(h)
app.UpdateButton.Text="Update"; app.UpdateButton.FontColor='k'; app.UpdateButton.Enable='on';
app.QuitButton.Enable='on';
drawnow
end
The above is one callback function in an app of mine -- when it finally(!) gets through all the error checking and the boilerplate, it then calls the external functions readBilling and writeAwards to do all the actual work.
As you see, there's nothing special at all in calling the functions; the m files do reside in the local folder where the app code is being built so they are visible, but that's it--using them is no different than using any other MATLAB function. When the app is packaged, the dependency walker finds them and includes them in the needed files section and all is well.
Nota Bene: the reference to the application local variables in the arguments; that's really the only thing at all about it being an application rather than "ordinary" procedural code.
Nota Bene Second: that the reference to the returned variables from readBilling are local variables within the callback function that are passed to writeAwards(); just like regular procedural code, they are local in scope within the function and are destroyed automagically when the function completes.
In this case writeAwards does all the work -- it creates a highly-formatted Excel file which is the end result of the application. If your app needs to compute stuff and do something with it inside the app, then like the helper function readBilling above, it will return those results locally and you do whatever is needed with them -- either in this routine or if they need to be available to other callbacks, then you create application variables for them.
It would seem you're trying to make it harder than it is...
Image Analyst
le 29 Jan 2022
0 votes
If it's a script, you can turn that script into a function and call it.
Or you can make it a function and paste it right in to your .mlapp file.
Just make sure you return the variables to the calling routine or else attach them to the app structure so that other functions can see the variables.
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!