Script for running (testing) another matlab script? - matlab

I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!

If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.

Related

MATLAB- Individually Saving Workspace Variables into Many Individual .mat Files

So I have many files in a MATLAB workspace all in the same format,
"project1day1", "project1day2" etc. and instead of having them all in the same workspace, I want to save them as their own individual .mat files with the same name.
So, I want the "project1day1" variable in the workspace to go to a "project1day1.mat" file.
I have 7 projects, and all of them except for project 1 has 3 "days". I was having trouble executing the exact syntax to do it. I want to loop through my workspace data in a general fashion. I want to execute something along the lines of:
maxdays=3;
maxprojects=7;
for i = 1:maxprojects;
for j = 1:maxdays;
save('project%dday%d','project%dday%d,i,j,i,j)
end
end
Two things:
1) The save option isn't working
2) I need to include some sort of ~if(exist '...') for the case where there isn't a 3rd day, but I'm having trouble doing so.
As rayryeng wrote, I think in most cases it would be better to either save the variables in one file, or (you wrote they are all in the same format) use a structure or a cell array, which makes it much easier to access them later.
If you really need to save all variables in the workspace to separate files you can do something like this:
vars = who;
for i=1:length(vars)
save([vars{i} '.mat'], vars{i});
end
But again, I wouldn't do this if it is not (for some reason) absolutely necessary!

clearing many MATLAB functions in one single file

I have many small functions in matlab, is it possible to put them all in one file so my work will look clearer?
i tried writing a small script and then adding the functions but is didn't work
any idea on how do do it?
It is not possible to have several functions in one file, because the function is accessed via the file name. That is why a function has to have the same name as the file name.
If you only access the "small" functions inside one other function, then you can put the small functions in the file of the other function, but then they are only accessible to this one function. This is called local functions in MATLAB. For example you have a file test.m with:
function x=test(y,z)
x = add(y,z)
end
function a=add(b,c)
a = b + c;
end
You can then only use add inside test, but you can use test just as usual.
What I usually do is put functions in subfolders. This helps you keep your path clean without any limitations. This also allows you to capsule your software better. The only thing you have to do is add the folder to your path with
addpath('subfolder');
If you have a function file, you can add other functions in that file.
If you have a script, you cannot add functions to it.
Note that if you put a function in a file, you cannot access the functions directly from outside your 'main' function scope.
In general I would recommend the use of folders, or proper file names to organize your functions, not stacking many of them in one file.
Extra
If your functions are really small and trivial, you could write a script with the declaration of anonymous functions for easy reuse. However this is probably not what you want.

is there a way to update workspace or variable editor values during running script without a break point

I would like to update variable editor at a predefined interval during a running script without having to use a break. Is this possible? I have a sim that runs for hours, I would like to be able to come back every hour and grab some values off a matrix in variable editor to play with in excel without interrupting running script. Any ideas would be greatly appreciated.
I think using assignin to copy your matrix to the base workspace should do exactly what you want. You'd need to manually reopen the variable in the editor to reflect the new data if it's changed.
If you wanted to get fancier, I imagine you could script evalin and openvar to do it for you, but I no longer have real Matlab to test with and Octave's fledgling GUI isn't there yet.

How to keep the work space of executing method in matlab?

I want to use script file as function because somehow, I need to define methods in script file and it is not possible in script file. However, when I defines script as function and execute it then I lose work space and I can not do further analysis of variables. I do not want to set breakpoints in code. Is there a way to keep work space alive for further analysis.
At the end of the function:
save filename;
evalin('caller','load filename');
I just got an evil solution:
%get all the var-names of the fcn-workspace
myVars=who('*')
for var=myVars'
assignin('base',var{:},eval(var{:}))
end
..this will work, but it is eval :)

MATLAB - Stitch Together Multiple Files

I am new to MATLAB programming and some of the syntax escapes me. So I need a little help. Plus I need some complex looping ideas.
Here's the breakdown of what I have:
12 seperate .dat files, each titled something like output_1_x.dat, output_2_x.dat, etc.
each file is actually one piece of a whole that was seperated and processed
each .dat file is approx. 3.9 GB
Here's what I need to do:
create a single file containing all the data from each seperate file, i.e. I need to recreate the original file.
call this complete output file something like output_final.dat
it has to be done in MATLAB, there are no other alternatives (actually there maybe; see note below)
What is implied:
I will have to fread each 3.9 GBfile into chunks or packets, probably 100 mb at a time (using an imbedded loop?)
these packets will have to be read then written sequentially
after one file is read then written into output_final.dat, the next file is automatically read & written (the master loop).
Well, that's pretty much it. I did a search for 'merging mulitple files' and found this. That isn't exactly what I need to do...I don't need to take part of a file, or data from files, and write it to a new one. I'm simply...concatenating...? This would be simple in Java or Perl, but I only have MATLAB as a tool.
Note: I am however running KDE in OpenSUSE on a pretty powerful box. Maybe someone who is also an expert in terminal knows a command/script to do this from the kernel?
So on this site we usually would point you to whathaveyoutried.com but this question is well phrased.
I wont write the code but i will give you how I would do it. So first I am a bit confused about why you need to fread the file. Are you just appending one file onto the end of another?
You can actually use unix commands to achieve what you want:
files = dir('*.dat');
for i = 1:length(files)
string = sprintf('cat %s >> output_final.dat.temp', files(i).name);
unix(string);
end
That code should loop through all the files and pipe all of the content into output_final.dat.temp (then just rename it, we didn't want it to be included in anything);
But if you really want to use fread because you want to parse the lines in some manner then you can use the same process:
files = dir('*.dat');
fidF = fopen('output_final.dat', 'w');
for i = 1:length(files)
fid = fopen(files(i).name);
while(~feof(fid))
string = fgetl(fid) %You may choose to parse the string in some manner here
fprintf(fidF, '%s', string)
end
end
Just remember, if you are not parsing the lines this will take much much longer.
Hope this helps.
I suggest using a matlab.io.matfileclass objects on two of the files:
matObj1 = matfile('datafile1.mat')
matObj2 = matfile('datafile2.mat')
This does not load any data into memory. Then you can use the objects' methods to sequentialy save a variable from one file to another.
matObj1.varName = matObj2.varName
You can get all the variables in one file with fieldnames(mathObj1) and loop through to copy contents from one file to another. You can then clear some space by removing the copied fields. Or you can use a bit more risky procedure by directly moving the data:
matObj1.varName = rmfield(matObj2,'varName')
Just a disclaimer: haven't tried it, use at own risk.