How to generate new numbers if i run the program everytime from the begining? - matlab

I am working on CATScript in optimization of a part.
When I run the script everytime it shoud provide numbers in ascending order.
For example if I run the program for the first time it should provide the output as " 1 "
and if I run the program again it shoud provide the output as " 2 " and so on.
I am stuck with this and I could not figure out th logic that we have to use here.
Looking forward for your help.
Thank you!!

An option (matlab based) could be to save a counter variable to a .mat-file at the end of the script, which is then loaded again at the beginning of the script.
That would allow you to keep track of how many times the script have been run.

In CATIA if it is being run multiple times on the same part/product, you could add a hidden, integer parameter to the specification tree and increment it each time the macro is run.
Another, more generic way would be to create a text file on the user's local and update the number in the text file.

Related

how to select single input from multiple output in Simulink?

Could any one tell me how can i use a Multiport switch in the following example. i can use alternatively MATLAB funtion block with M script but its slow. 2 days before i try to use a sum block as shown in figure2 and i thought i am genius and and i was succed. But Today Morning i am getting a 'Ghombaro Result'(Unexpeted Result)' i.e during simulation mode when i am changing my input level the pervious output is remain same and its adding up i thought it will replace it with ZERO ... :( ..... is there any way to do it by using another block or with any idea...
.
Thank you...............
Use a Merge block, to merge all input from your 'If Action Subsystems', set Initial Output as 0.
Connect the output of merge block to the 'Display' block

fopen error - works for a while but then gives an error

I have a script that is running a series of for loops, and within these for loops a file is created that is then run using an external program using the script command. In summary it looks like this:
for i=1:n1
for j=1:n2
for k=1:n3
fid=fopen('file.txt','w');
fprintf(fid,'Some commands to pass to external program depending on i j k');
fclose(fid);
system('program file.txt');
end
end
end
The script has in total about 500k cases (n1xn2xn3), and runs fine for a small scenario (about 100 runs), but for the entire script it runs for a while and then returns an error for no apparent reason, giving this error:
fopen invalid file identifier object
There is no obvious reason for this, and Im wondering if anyone could point out what is wrong?
Just a guess: an instance of your external program is reading file.txt and at the same time the next iteration of your nested loop wants to open file.txt for writing. The more instances of your external program are running at the same time, the slower your machine, the more likely becomes this scenario. (called a 'race condition')
Possible solution for this: use a separate text file per case with a unique file name
You should also consider using other ways to call your external function because file handling for 500k cases should be very slow.
Hope that helps,
Eli

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.

Script for running (testing) another matlab script?

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.

How do I suppress the DOS window from Matlab?

I have 'inherited' Matlab code (A) that uses another compiled Matlab code (B). I do not have the source of B. B requires user intervention ('Hit return to continue'), and I need to use A in a loop. I need to do something so I would not need to hit Return each and every time until the loop is done.
The command I use in the loop is:
str='!start "Code_B" /low "c:\Code_B\bin\Code.exe" r';
eval(str)
Are there any other switches that I can use to suppress the call to 'Hit return' ?
Thanks
Katto
One way you could do this is to create a batch file that:
Starts the compiled Matlab program
Waits for the program to run (fixed delay?)
Uses a utility to send the program an Enter key
There are many (free) utilities that let you send keystrokes to a program.
Instead of calling Program B, you would call this batch file.
You can create a text file, let's say autoreturn.txt, with many empty lines (just end of line characters), over the number of loops you expect. Then add redirection of input from this at the end of your string:
str='!start "Code_B" /low "c:\Code_B\bin\Code.exe" r < autoreturn.txt';