I've got a problem changing my code result. The main problem is when I run the code, it works very well, and the final results are accurate. However, as you can see it's like a division of 2 big numbers. Please help me out how to change the formation of the results. I have to say that I've already used **format command ** and didn't get anything.
the result I want is something like :
sigma=156.45e+6;
Thanks.
The format command isn't what you need here I think.
If these values were generated using the symbolic toolbox, then they tend to remain as whole fractions, and in order to change this, you simply need to run the following code either in your script or in the command window:
sympref('FloatingPointOutput',true);
This will produce the values you are looking for.
Alternatively you can cast the values you have to a double using the following code:
ans = double(ans);
sigma1 = double(sigma1);
sigma2 = double(sigma2);
You must have set the command window format to long some time ago as this is not the usual behavior.
You can change this simply by typing to the command window.
The defaults are:
format shortEng % number representation
format compact % line spacing
BTW, you can get your current setting with this command
get(0,'Format')
However, changing the format only applied to your current session (until you close MATLAB). Therefore it is odd that you ask. If it persists to be changed, someone must have fiddled with the preference
The specified format applies only to the current MATLAB session. To maintain a format across sessions, choose a Numeric format or Numeric display option in the Command Window Preferences.
Related
I tried to use the linearization function in Dymola, but it seems when the result's dimension is large, Dymola won't show the result.
My question is:
How could I print the result or where to find it?
What you can do, is assign the result to a variable. This can be done using the Outputs group as shown in the screenshot below. If you e.g. enter "sys" in the field for ss, you will get a record sys, in which you can access the matrices/vectors by typing sys.A, sys.B etc., which I've tested for a system of size 200x200. Typing this into the command line will display the content. Of course this record not only for outputting it, but also for post-processing.
The only thing this actually does, is modify the call from Modelica_LinearSystems2.ModelAnalysis.Linearize("ModelName") to sys=Modelica_LinearSystems2.ModelAnalysis.Linearize("ModelName"), so it can be done in the Commands window as well.
Call the function from the command line and capture the output. Then you can do with it what ever you want.
Everything you find in the Linear Analysis toolbar is part of the library Modelica_LinearSystems2. The Linearize item in this menu calls the function
Modelica_LinearSystems2.ModelAnalysis.Linearize("<your-model>")
which is also printed to the command line. The function returns the operator record Modelica_LinearSystems2.StateSpace, which contains all the info you are interested in. The default behavior of Dymola is to call the String method of this operator record and print it to the command line. If you look at the source code of Modelica_LinearSystems2.StateSpace.'String' you can see this at the start of the algorithm section:
// If system is too large, do not print the matrices
if size(ss.A,1) > 50 or size(ss.B, 2) > 50 or size(ss.C, 1) > 50 then
...
On the command line you can capture the operator record in a variable like this:
stateSpace = Modelica_LinearSystems2.ModelAnalysis.Linearize("<your-model>");
And then access the values on the command line via
stateSpace.A
stateSpace.B
stateSpace.C
stateSpace.D
For a nice html report you can also pass the operator record to one of the analysis functions:
Modelica_LinearSystems2.StateSpace.Analysis.analysis2.printSystem(stateSpace)
This creates the file systemAnalysis.html in your working directory, containing a nice visual presentation of your system.
I am working on this project which requires analyzing a large (>50GB) dataset in a server, both in Stata and MATLAB. Both parts are required and I cannot use only one of them.
My ultimate goal is to generate a .tex file named something like commands.tex which looks like this:
\newcommand{\var1}{val1}
\newcommand{\var2}{val2} % MATLAB file matlab_file.m on DD/MM/YYYY
\newcommand{\var3}{val3} % Stata file stata_file.m on DD/MM/YYYY
...
where variables are ordered alphabetically and each of the values is most probably a number. Note that the commands in the comments would help me trace where did I generate the values. The usage of the file is so that after a preamble I can use LaTeX on the following way:
<preamble>
\input{commands.tex}
\begin{document}
Variable 1 has a value of \var1 and variable 2 has a value of \var2.
\end{document}
The purpose of this is so that I can analyze locally (or remotely) a sample, say of 0.1 or 10 percent of the total observations, write a report with those, and then run the analysis again with a bigger size. I want to completely eliminate the chances of me copying a number wrong.
I am trying to write some code both in MATLAB and Stata, but I think that is beyond my expertise, and would be very grateful if someone could help me figure out how to do it. To be honest, I feel I would be able to do the MATLAB part but the Stata I have no idea.
Stata code
What I am trying to do is to generate a command that takes as an input a name and a scalar and as an output defines the corresponding variable in my commands.tex file detailed above. My goal is to be able to generate something like this:
sysuse auto
reg price weight
define_variable PriceWeight = _b[weight], format(%4.2f)
and what I hope the code to do is that:
If \newcommand{\PriceWeight} does not exist in commands.tex then it adds its value to the list, preserving the alphabetical order.
If the variable exists then it deletes its value and rewrites above it, with the value given in the scalar.
I know how to give the values to a program in Stata, but I do not exactly know how to use those values and perform the necessary commands. The syntax is something like:
program define define_variable
syntax anything = X, [format(string)]
<other code>
end
Note: Of course, I need something way deeper than regression coefficients, but as a simple example this would suffice.
MATLAB code
This seems to be easier in MATLAB, but I do not know exactly how to automate the process. In MATLAB what I want to be able to do is something like:
clc; clear;
PriceWeight = 3
define_variable('PriceWeight',PriceWeight,format)
again where it automatically goes to the single file and updates it accordingly. Any hel[p with be very much appreciated.
Based on your comments and assuming that your file with all relevant variables is not huge, I would suggest getting your data from Stata to Matlab, and update your variables there as necessary (using functions such as exist or strcmp if you have a list of names). A quick google search gives me this link for Stata to Matlab.
To make it easy to process you might want to create a cell (I will call C), where one column contains all variable names and one column contains the scalar values.
Then, once you have assembled all your variables, you can sort your cell alphabetically and write it to a file using this.. Of course you would write a .tex file, and then iterate over your cell with something like
fprintf(fID,'\newcommand{\%s}{%f} ',C{i,1},C{i,2})
I hope this is understandable and helps.
I would like to remove whatever I just typed - and the last shown command - from the matlab console display. Needless to say, this would be ideal for pranksters (but this is of course strictly for academic purposes only). This is as far as I have gotten (based on this related answer):
hist = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory; %get history
last = strjoin(cell(hist(end-2:end)),' '); %convert history to string
fprintf(repmat('\b',1,numel(last))); %replace characters of string with whitespace
However I can only access the last typed command (through the command history) - not the last displayed command (which would be ideal). Any ideas how to solve this?
Disclaimer: I don't advise doing this.
The MATLAB CommandWindow contents are stored as a CmdWinDocument, which is an extension of the Java PlainDocument type, and an interface of the Document type. The current window can be accessed using the command:
com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
In theory, you should be able to remove text from the command window using something like:
doc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
endpos = doc.getEndPosition
doc.remove(endpos-10,10)
which would, in theory, remove the last 10 character from the document. You may have to call the removeUpdate function as well. Obviously problems will be caused by the fact that these commands will be appended to the document during this process. I have not tested this, and you're likely to cause problems with internally stored offsets within the CmdWinDocument class, so use at your own risk.
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.
Is it possible to retrieve previous results in the command window? For example i had some matrices and vectors in the command window and after an error the matlab was closed . Is it possible to retrieve them?
Thanks
No guarantees about what happens after a crash, but in general, use the command diary('filename.txt') to append all text in the CommandWindow -- not the Command History window -- to that text file.
(I dunno why MathWorks doesn't call the "CommandWindow" what it is, i.e. the Console, but there you are.)