How to run several .m files by creating a master .m file - matlab

I have a series of .m files that plots Power spectral density, one third octave band levels, spectrograms etc. Each one of them is a seperate .m file (function file to be specific) that is executed by one main .m file which opens up a GUI that interactively lets us choose what we want and then it gives us the outputs accordingly. The thing I want to do now is create a small script that will entail to initiate to run these series of .m files and store the results so that I can use these results (probably by saving them as .mat files) to plot further results.
Any suggestions or ideas are welcome! Many thanks.
Further to the above, please find below details -
so can save the myOutput (referring to Wolfie's comment) as and when the function is being called within the main .m file? The thing with the GUI is it does RUN based on user's selection to analyse .wav files. suppose i choose one file then it does the analysis according to what i want and gives me outputs. suppose i want to do the analysis for batches of .wav files at a time, then it is taking lot of time. i wish to know if i could write another script to direct this main .m file to do the analysis of required things in batches and store the results so that in the end i can use all the stored results to plot as to what i need to plot. hope this is helpful. thanks all.

You can run MATLAB scripts from command line or code with the run function.
Suppose you have an *.m file stackoverflow_playground.m with some content you can let that script run by
run('stackoverflow_playground.m')
So one master script could contain several run statements after each other to run consecutively the desired scripts given their position in the code. This master script could then also include some save routines for the obtained results.

Related

How to load .mat files onto Matlab? Basically what's wrong with my code?

For this project we have been given code, and will be changing some inputs and assumptions. Thus, I already possess the original codes, but just changing all the creator's file paths to match my own computer is yielding me a lot of trouble. The following, and many variations of, continually yield errors.
load \Users\myname\Library\Documents\...
The error is
Error using load
'Unable to read file
\Users\myname\Library\Documents...'.
No such file or directory.
My files are stored in my Documents. Another person in my group on windows has used
load C:\Users\hisname\Desktop\...
Is there something I'm missing in my line, similar to the C drive but on Mac? Is my code just completely wrong, I'm able to load files in R quite easily, but Matlab is posing a huge hurdle. I have no experience with Matlab and have been asked simply to run this code.
On the Mac, path components are separated by /, not \. Thus, you should type
load /Users/myname/Documents/filename.mat
You can use the location bar at the top of the command window to change to the directory where your file is located, and then you can type
load filename
to load filename.mat.
Also, are you sure you have a Documents directory under Library? Why?
To run code from a file called "my_file.m", than just open your Matlab and type run my_file.m. This will run your script in the Command Window.
The load function is used, if you want to load a .mat file. These are normally files, where variables from your workspace are stored.

Uploading multiple .txt files at once on MATLAB GUI

I would like some guidance on how to import multiple .txt files containing data seperated by comma on a MATLAB GUI. Once the files are uploaded, I have a function that will manipulate all the data from each .txt file.
Any help is appreciated.
The easiest way to import multiple files is to:
Use the file importer GUI in matlab and generate a script after you
selected your preferred parameters
Generate a script (there is a button to generate a script in the importer)
Modify the script with a for loop to load multiple files and save them in a variable (a cell array can handle different sizes of data in each file)
Try uigetfile to launch a dialog for loading files. Set 'MultiSelect' to 'on' in order to select multiple files at once.
Here's an example call:
[filenames, pathname] = uigetfile({'*.txt; *.csv','Comma separated values';...
'*.*','All files'},'Select files','MultiSelect','on');
You will need to check if the user actually selected a file or if they canceled.
If I understand your question correctly, you have a GUI already. In this case you just need to add the above call to your designated callback function (i.e. whatever you click to invoke this file load interface).

How to call a function in Matlab when two programs take input data from different directories?

I am trying to read output of one program into another program. But in each of these programs, I am using some input data which is being loaded from those respective directories. For example,
In program 1, I load some text files and run the code for some array of outputs.
In Program 2, I load some other text files and run the program. And in program 2 I also want to use data which is output of program 1.
Text files in program 1 and 2 are at different locations.
Can you suggest me to handle this issue?
As mentioned in the comments, you only need to use the absolute path to your files:
load('C:\...\dataFolder\dataFile.mat')
This way you can reach it independently on the current working folder in MATLAB.

Multiple pathdef files in Matlab?

Suppose two different Matlab users share a computer and they each want to be able to save and load their own Matlab paths. (Or, a single user wants to use different paths at different times.) What's the easiest way to handle this?
Should there be multiple pathdef files? Alternatively, should they each have a script that calls restoredefaultpath and then uses addpath to add new paths?
You can use the startup.m file for that.
When starting up, Matlab executes the file matlabrc.m, which is the master startup file, and is common for all users. Among other things, this file
Sets the first entry of the path as the user-folder of the current user, that is, the user that started Matlab. (This is done by calling pathdef, which in turn calls userpath); and then
Looks for a startup.m file in the path, end executes if it exists.
Therefore you can place a user-specific startup.m file in each user's folder, and Matlab will run the appropriate file depending on which user started Matlab. In that file you can set the path on a per-user basis, and do other user-related stuff.

*.m file requires external *.mat file for input. Not sure how to implement

Long time reader first time poster!
I am having the pleasure of learning more advanced MatLab by going through my Professors code, which is proving to be a little difficult.
At the moment we have a GUI which runs some functions on external data and produces some graphs as an output. I would however like to run the function by itself without having to use the GUI.
The function heading looks like this...
function info = fn_2d(exp_data, data, options)
Where exp_data is a structure *.m file, data is a function stored in the m file itself and options is, well, I'm not sure, but the inputs come from the GUI (such as coordinates of mouse clicks etc).
I'm a little out of my depth but I am going through the code line by line.
Could anyone help me with how to read the exp_data directly from the directory. I tried to add this and excluded exp_data from the input arguments for the function, but it didn't work.
exp_data_file = ('Example.mat');
exp_data = matfile(exp_data_file);
Again, apologies for being quite so out of my depth.
R