Launch a custom App Designer application when opening a file.

9 views (last 30 days)
I am looking for a way to program my data files, that I generate and save via a custom App Designer application, in such a way that they can launch the custom App when the data file is accessed via Windows Explorer. The behavior that I am trying to simulate would ideally be similar to opening a MATLAB file type such as a *.fig or *.mat file from WinExplorer when MATLAB has not yet been launched. In this case, MATLAB will launch first and then continue to open the original file.
My custom App generates and saves files, but I would like to have accessing the *.mat files (or other file type if better suited) launch the custom App if it has not already been done. I honestly have no idea where to begin for this type of programming, if it is even possible in MATLAB.
Additionally, the custom App would be deployed as a standalone executable (*.exe) on computers not having MATLAB installed, and file types associated with MATLAB (*.fig, *.m, *.mat, etc.) do not have an obvious means of initiating the app. I would also expect that the app would require a check on startup to determine how it was called in order to determine the approach load/open callback to use and to continue opening the file originally selected by the user.

Answers (1)

Harsh Mahalwar
Harsh Mahalwar on 26 Feb 2024
Edited: Harsh Mahalwar on 26 Feb 2024
Hi Allen,
I can understand that you are trying to save your data in such a way that when the saved data is launched through the file explorer, it opens your custom MATLAB app.
Unfortunately, you cannot call your custom MATLAB app directly through your saved “.mat” file. A workaround can be to create a “.m” file as a primary data reference which would call your MATLAB app and load the data from “.mat” file at the same time.
Here’s an example for the same:
p = rand(1,10);
q = ones(10);
% saving the sample “.mat” file
save("pqfile.mat","p","q")
% This line tries to open the given file, otherwise
% creates it and then opens it, if it doesn’t exist
FID = fopen("pqfileScript.m", "w");
customScript = "load pqfile.mat; myCustomApp(p, q)";
% Write to the sample “.m” file
fprintf(FID, '%s', customScript);
This script will save a “.m” and “.mat” file., the “.m” file can be used to load data and start the custom app. However, this method can be too tedious for the end user as they will have to run the “.m” file again and again.
Another alternative can be to use “.bat” files (for windows only) as primary data reference.
Here is an example to demonstrate how you can achieve this:
p = rand(1,10);
q = ones(10);
% saving the sample “.mat” file
save("pqfile.mat","p","q")
% This line tries to open the given file, otherwise
% creates it and then opens it, if it doesn’t exist
FID = fopen("pqfileScript.bat", "w");
batScript = "@echo off";
line1 = convertCharsToStrings('start "" "path to your .mat file"');
line2 = convertCharsToStrings('start "" "path to your custom application"');
batScript = batScript+ newline + line1 + newline + line2;
% Write to the sample “.bat” file
fprintf(FID, '%s', batScript);
The above MATLAB script creates a “.bat” file that loads the data and launches the app (it can be easily added to your MATLAB apps code). You can also add parameters (in case you have any in your MATLAB app’s startup function) through “.bat” files, here’s a thread I was able to find on how you can do so,
Please refer to the following MathWorks documentation for more information on the “fopen” function:
I hope this helps, thanks!
  2 Comments
Allen
Allen on 27 Feb 2024
Hello Harsh,
Thank you for your reply. This approach is really not much different from what I am currently doing within the custom app. I will include more details to my original question, since I noticed I did not indicate that the custom app would be deployed on computers as an *.exe and that do not have MATLAB installed. Thus, *.fig, *.mat, *.m files, etc. have no programs associated with them.
Thanks,
Allen
Harsh Mahalwar
Harsh Mahalwar on 2 Mar 2024
Edited: Harsh Mahalwar on 15 Mar 2024
Hi Allen,
With MATLAB you can also save your data in *.txt, *.csv, *.xlsx format, etc.
Here's the revised code that might suite your workflow better:
% Code that executes after component creation
function startApp(app)
try
app.rs = readtable("savedData.csv");
disp(app.rs);
catch
warning("No presave data found!");
end
end
% Button pushed function: SampleAppButton
function saveRandData(app, event)
% Saving sample data in form of a *.csv
rTable = table(rand(10, 10));
writetable(rTable, "savedData.csv");
% This line tries to open the given file, otherwise
% creates it and then opens it, if it doesn’t exist
FID = fopen("savedData.bat", "w");
batScript = convertCharsToStrings('start "" "C:\Users\Harsh Mahalwar\Desktop\mySampleApp.exe"');
% Write to the sample “.bat” file
fprintf(FID, '%s', batScript);
end
Here, I have added an exception handling to the application which basically looks for the pre-saved data and gives a warning if it's unable to find it.
Run the "savedData.bat" to load their data along with the application.
You can also add parameters from the batch file itself and handle them in MATLAB (This way you can also track if the app was called from the "savedData.bat" or not and avoid showing warning if the application was loaded normally).
Here's an example on how to do so:
% Code that executes after component creation
function startApp(app, isBatch)
if nargin == 1
try
app.rs = readtable("pqData.txt");
disp(app.rs);
catch
warning("No presave data found!");
end
else
disp("Application loaded");
end
end
% Button pushed function: SampleAppButton
function saveRandData(app, event)
% Saving sample data in form of a *.csv
rTable = table(rand(10, 10));
writetable(rTable, "savedData.csv");
% This line tries to open the given file, otherwise
% creates it and then opens it, if it doesn’t exist
FID = fopen("savedData.bat", "w");
% This would help us not show the warning in case the
% application is loaded normally.
line1 = "set arg = 1";
line2 = convertCharsToStrings('start "" "C:\Users\Harsh Mahalwar\Desktop\mySampleApp.exe" %arg%');
% Write to the sample “.bat” file
fprintf(FID, '%s', line1 + newline + line2);
end
I hope this helps, thanks!

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!