I have a matlab program that generate a matrix f (which is 3 column and 100 raw) then the program plot the 1st column with second one and third one
every time I change a specific variable the matrix values changes and the plot changes
if I made a for loop to generate different matrices with different plots how could I made the program to save the matrix and the plot figures each loop with different names in the same folder
You can save the matrices inside the loop or place them in a cell array and save that after the loop. To save figures to file you can use print.
Something along these lines:
for ii=foo:bar
mat=...;
fig=figure;
% somehow create matrix mat and figure fig;
save(['quux',num2str(ii),'.mat'],mat);
print(fig,['quux',num2str(ii)]);
end
Related
This is a fairly simple thing I think, but I cannot seem to get the right output that Im looking for. I am using matrices to represent state space models in simulink, and I am trying to get my states output to the workspace,
it is a simple 4x1 vector, and I tried just using the regular "to workspace" block, but it seems it concats to either a 2d or 3d vector..
I want to have a tx4 matrix output that I can reference the first state and plot for all simulation time(t) like x(:,1), the second state x(:,2) etc...
You can set a save format in a To Workspace block. Default this is set to timeseries, but you can set it to Array.
Looking at the doc for the Array setting:
If the input signal is a scalar or a vector, each input sample is output as a row of the array. Suppose that the name of the output array is simout. Then, simout(1,:) corresponds to the first sample, simout(2,:) corresponds to the second sample, and so on.
You want the first dimension not to be time, but your state vector, so transposing simout should do the trick.
simout = simout.'; % or tranpose(simout);
I am plotting stem plots in given figures within a function using the following code...
% plot - phase = 1,2 or 3, with different data each time
% Each phase is called more than once
figure(phase);
stem(1:length(phaseSystem),phaseFailureTimes);hold on
This function is called several times within a loop, plotting on the same figures iteratively.
I want each plot to be calculated but not shown until a later time. This is because the figures currently show and update live, which is slowing down the script. I'd rather calculate but hide them, as opposed to storing all the data and plotting them at the end.
Thanks
I think you need a bit of reorganizing so that your function outputs the 3 values for your variables phaseSystem and phaseFailureTimes as n by 3 matrices.
Let me call this function calculate_phase_failure. Then the script/function that calls calculate_phase_failure could accumulate the results. Finally, a separate loop at the end could generate your plots. If the number of elements are different for each iteration of your loops you might need to use a cell-array to accumulate your results.
Here is an example for the simplest case where the number of elements is consistent between iteration of your loops.
for i=1:n
[phaseSystem(:,:,i), phaseFailureTime(:,:,i)] = calculate_phase_failure( <input variables> );
end
% now generate your plot
for i=1:n
for phase=1:size(phaseFailureTime,2)
figure(phase);
stem(1:size(phaseSystem,1), phaseFailureTime(:,phase,i))
end
end
I have text files that contain two columns with numbers. Over a for loop, I store the first and second column as X(n) and Y(n) respectively (as floats), n being the iteration number.
Let's say that I don't know how many files I have and that the length/range of the data is variable.
Is there a way to create a sort of dynamic variable so I can use it as an input to graphically represent the data like
plot(dynamic_variable)
instead of writing per hand
plot(X1,Y1,X2,Y2,...,XN,YN)
I know there should be the possibility to interpolate the data (since the files haven't the same length/range) so it is possible to create two matrices, let say XM and YM, and finally write (XM,YM), where
XM = [X1_intrpl X2_intrpl ... XN_intrpl]
YM = [Y1_intrpl Y2_intrpl ... YN_intrpl].
Is there a more direct way to do it?
I am far from being an expert: so I would also appreciate any comment and/or criticism on my idea/approach.
The Matlab plot function does not seem to support what you are looking for. I guess you have already checked the documention on the plot command here:
https://de.mathworks.com/help/matlab/ref/plot.html?requestedDomain=www.mathworks.com
What you can do is write your own plot function that takes both matrices as parameters.
In the function you would loop over the pairs in the matrices plotting them using hold on to display all the data in one plot.
One option would be reading in each set of X(n) and Y(n) into a cell array such that,
X{1} = X1
Y{1} = Y1
...
X{N} = XN
Y{N} = YN
Then to plot, rather than trying to merge everything into a single array, you can simply plot each set of X and Y one at a time onto the same figure.
%Instead of:
%plot(X1,Y1,X2,Y2,...,XN,YN)
%Use:
figure()
hold on
for i=1:N
plot(X{i},Y{i})
end
I have a single *.txt file with multiple records, from an MPU6050 connected to Arduino.
I can recognize when a new record was made because the column time restart from a random value lower then the previous (is never 0).
The file is a nX7 which contains in order time, ax, ay, az, gx, gy, gz
I am trying to extract the m subrecords from the single matrix, so i defined a logical if.
If time i > time i+1 keep track of the position from the original matrix and store in a submatrix (rangematrix) this boundary values.
From the rangematrix create a set of submatrices (the total number of submatrices will be [size(rangematrix,1)-1] .
I have a civil engineer background and i am a noob with Matlab.
Thanks for your time, thanks for you patience.
I tried to solve this with the code below, but I think is only rubbish.
%Open the file
filename= uigetfile ('.txt');
fileID = fopen (filename);
logmpu6050 =csvread(filename);
fclose (fileID);
n=length(logmpu6050);
%Count every time i>i+1 where i is the i,1 element of my dataset
for i=1:n-1
%Save the data of the i raw every time happens i>i+1
if logmpu6050(i,1)>logmpu6050(i+1,1);
rangematrix(i,:)= logmpu6050(i,:);
end
end
% Create a new sets of matrices from boundary values
I also read a lot of questions on stack but i didn't find the solution:
MATLAB: extract every nth element of vector
Extract large Matlab dataset subsets
MATLAB: Extract multiple parts of a matrix without using loops
MATLAB: Extracting elements periodically
Extract data from MATLAB matrix without for-loop
How to extract a vector from a large matrix by index in MATLAB?
How to extract a part of a matrix with condition in Matlab
You can use diff.
filename= uigetfile ('.txt');
fileID = fopen (filename);
logmpu6050 =csvread(filename);
fclose (fileID);
n=length(logmpu6050);
%Count every time i>i+1 where i is the i,1 element of my dataset
rangematrix = logmpu6050(diff(logmpu6050(:,1)) > 0,:);
I am creating a program where the user can select multiple files to plot and compare data. The program can properly graph the data, the problem I have encountered is within the legend.
I tried posting an image, however I do not have a high enough reputation. So I will try to explain the graph in detail. Two sets of points are plotted (two matrices of different sizes). The curves are labeled by the user, and in this example they are: "PS, Cs" and "PS, Po."
The program successfully plots the "PS, Cs" curve with the red squares then plots the "PS, Po" with the blue circles however the legend continues to show the red squares for both sets of points. Below is the loop within the code that does the plotting.
fig = small_group_struct;
mystyles = {'bo','rs','go'};
mat_len = size(small_group_struct,2);
for q = 1:mat_len
plotstyle = mystyles{mod(q,mat_len)+1};
semilogy(1:size(small_group_struct(1).values),small_group_struct(q).values,plotstyle);
hold all;
[~,~,~,current_entries] = legend;
legend([current_entries {small_group_struct(q).name}]);
end
hold off;
%legend(small_group_struct.values,{small_group_struct.name});
Other threads that I have seen suggested putting the plot command into a handle but since each set of points is a nxm matrix of different sizes, the program does not like this.
Also, as mentioned at the beginning the user will select the number of files and while this is typically two, it will not always be the case and why I am trying to plot it within a for loop.
Any suggestions and comments would be greatly appreciated.
EDIT: I now have a high enough reputation to post images, so here is a screenshot of the graph
You can use handles to specify what labels go with what data in the legend.
You say that "each set of points is a nxm matrix of different sizes." Plotting an mxn matrix creates n line objects and returns n handles. You can keep track of all of these handles and assign labels to them when you create the legend.
Here's an example:
% Cell array of data. Each element is a different size.
data = {rand(100, 1), rand(150, 2)};
styles = {'ro', 'gs'};
% Vector to store the handles to the line objects in.
h = [];
figure
hold on
for t = 1:length(data)
% plots the data and stores the handle or handles to the line object.
h = [h; semilogx(data{t}, styles{t})];
end
% There are three strings in the legend because a total of three columns of data are
% plotted. One column of data is from the first element of data, two columns of data
% are from the second element of data.
strings = {'data1' ,'data2', 'data3'};
legend(h,strings)
You might want to do something different with the legend but hopefully this will get you started.