Live display of variables during MATLAB simulation [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I have a simulation which has numerous iterations. There are variable matrices whose values I would like to monitor while the simulation is happening. What are good ways of doing this?

I have two tools that I regularly use for something like this.
1. Iterative output that erases with each iteration
This works well for an optimization where you have a scalar function output you want to monitor.
Before my while loop, I'll define screenOut = []; and an output formula in screenOutFormat. In my loop after the computation has happened for that iteration, I'll put something like
if iter <=2
bspace = [];
else
bspace = repmat('\b', [1 length(screenOut)]);
end
screenOut = sprintf(screenOutFormat, iter, estimatedValue);
fprintf([bspace screenOut]);
This way you get to see the starting point and each iteration without destroying your command window history.
2. Dynamic updates to a figure
This works well if you have something you can plot.
Before the loop begins, set up a plot with
figH = figure();
progPlot = plot(Y);
Then with each iteration you can do
progPlot.YData = Y;
drawnow();
This way you continue plotting the estimate you care about.
If you have a matrix that you care about, I'd recommend picking off the elements that you're really interested in and using #1. But if there's something more useful you can plot, #2 is usually more interesting to watch during long estimation/simulation routines.

Related

Dont understand the function of cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f')); at all [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
IND_STRT = 0;
ALL_STRT = IND_STRT:12:510;
cmd_data = zeros(length(ALL_STRT),1); %example: x=zeros(1,21) gives you a 1 by 21 matrix
for ii = 1:length(ALL_STRT) %declare variable ii to run from the row of length
if ~isempty(data{i})
cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
end
end
I need to read the EPS from EnduroSat, however i have difficulty understanding the line cmd_data(ii) = cell2mat(textscan(char(data{i}(ALL_STRT(ii):(ALL_STRT(ii)+4))),'%f'));
Im required to utilised MatLab to code and this particular line have an error and i don't understand why.
Whenever you see a complicated line like this in MATLAB, try to break it up.
% find some indices. These values have been selected by the programmer/data, can't say why.
a=ALL_STRT(ii):(ALL_STRT(ii)+4)
% obtain that articular section of the data
b=data{i}(a)
% convert it to a char data type (characters)
c=char(b)
% scan text, and treat them as float
d=textscan(c,'%f')
% the output is a cell array, we want a matrix instead. Make the cell array into a matrix.
cmd_data(ii) = cell2mat(d)
You can read particularly what each of these do better in their documentation pages, and you can see it work if you put a break-point in the code, and see what each of this parts output when you call it. Learn how to debug, is a very very powerful tool

Array indices must be positive matlab [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new to MATLAB, I have created a triangle, I used the code as below to find all coordinates of x,y.
side_1=[linspace(3,5,100);linspace(2,3,100)]
side_2=[linspace(5,4,100);linspace(3,5,100)]
side_3=[linspace(4,3,100);linspace(5,2,100)]
all_coordinates=[side_1,side_2,side_3]
I used the code as below to find angles.
angles=zeros(300,1);
for i=1:300
angles(i)=atan(all_coordinates(2,i)/all_coordinates(1,i))*180/pi
end
Since view point is flatview, y=0, x=angles
I used this code to plot angles.
for i = range(length(angles))
scatter(angles(i),0)
end
Got error array indices must be positive integers or logical values.
Not sure what you are trying to do with the loop but the problem is that you last loop is wrong.
range(length(angles)) =0, you should use for i=1:length(angles) instead
In addition the two loops are unnecessary. Not sure what you are trying to do with the scatter plot, but you can just write:
angles=atan(all_coordinates(2,:)./all_coordinates(1,:))*180/pi
figure
scatter(angles,zeros(1,length(angles)))

Creating a mean square error function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I'm really new to MATLAB and have to implement some functions. To start, I'm supposed to make a function about a mean square error, for which I have the following formula:
My goal is to implement this in MATLAB. I've tried to analyse this for days, but I'm still lost. I guess the first thing to do is to create a function and then put all of the calculations. Should I declare variables like N though? Or can I just use them in the calculations without declaring? I'm just looking for some advises that would help me get started, I have no one else to ask about this, so a short guide/tips on how to take down this particular example would be amazing. Thank you in advance!
For a mean square error you need two inputs, Y and Y_bar and one output E. You don't need to declare N because it is implied by the length of each of your inputs. I'm going to assume that your inputs are both column vectors and are the same length.
function E = MSE(Y, Y_bar)
N = size(Y,1);
E = sum((Y-Y_bar).^2)/N
end
You should save this code a an .m file named mse.m and make sure it is in your working directory. If you don't know what that means, you need to look it up.
This is extremely basic MATLAB though, before you continue with anything, I think you should start by working through some beginners guides. As mentioned, stackoverflow is not the place to learn the very basics of a programming language.

Why is Matlab creating a second ghostly instance for my variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on a matlab program where at one point I create a 3x1001 matrix called 'bots'. I checked that Matlab created this matrix correctly, but as soon as Matlab enters a while-loop, there is an extra matrix called 'bots' of dimensions 1x1001 with different doubles in it.
...
bots=zeros([3 1001]);
bots(1,:)=botsStartPlace;
bots(2,1:nbBots-1)=botsStartPlace(2:nbBots);
bots(2,nbBots)=length(indexTable);
whos bots % bots 3x1001 24024 double
while(going)
whos bots %bots 3x1001 24024 double
%bots 1x1001 8008 double
....
Anyone has a clue why matlab is doing this? It's freaking me out! :p
The following snippet does not create a "ghostly" instance:
bots = zeros(3, 1001);
whos
going = 2;
while (going)
whos
going = going - 1;
end
The output is:
bots 3x1001 24024 double
going 1x1 8 double
So the problem cannot really come from the while-loop itself.
I have found the problem! My bad, there never was a ghost! Matlab had already gone through the loop once and was ordering the 'whos' output of different calls to the function together which gave me the impression that there where multiple instances. Instead I was just accidentally hurting the matrix in the loop and thereby changing it's dimensions! Sorry for your time! :(

syntax of Iteration in K-mean Clustering With MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have a problem with using the number of iteration in MATLAB.
i use this code but there is no change in the number of iteration:
Edit
this is my code...
clc;clear;close all;diary temp;
cluster_data=unifrnd(-3,+3,[100 2]);
optns = statset('MaxIter',500);
[idx,ctrs]=kmeans(cluster_data,3,'dist','city', ...
'display','iter','replicate',100,'options',optns);
figure
plot(cluster_data(idx==1,1),cluster_data(idx==1,2),'r.','MarkerSize',14)
hold on
plot(cluster_data(idx==2,1),cluster_data(idx==2,2),'b.','MarkerSize',16)
plot(cluster_data(idx==3,1),cluster_data(idx==3,2),'g.','MarkerSize',18)
plot(ctrs(:,1),ctrs(:,2),'kx',...
'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Cluster 3','Centroids',...
'Location','NW')
fid=fopen('temp');
dat=textscan(fid,'%s');
fclose(fid);
delete temp
dat=dat{1};
i1=find(~cellfun('isempty',strfind(dat,'sum')));
ie=find(~cellfun('isempty',strfind(dat,'iterations')));
i1=i1(1)+1;
Nd=str2num(dat{ie(1)-1});
ie=Nd*4+i1-1;
dat=reshape(str2num(strvcat(dat{i1:ie})),4,Nd)';
iter = dat(:,1) % <-- iterations
sm = dat(:,4) % <-- sum
figure
plot(iter,sm)
the issue is the number of iteration... how can i really increase the number of iteration?
however i increase the 'Maxiter' but no change appear.
You should use statset to change the number of iterations:
optns = statset('MaxIter',500);
And the call kmeans like this:
[idx,ctrs]=kmeans(cluster_data,3,'dist','city', ...
'display','iter','replicate',100,'options',optns);
Comment
The documentation for kmeans states:
MaxIter - Maximum number of iterations allowed. The default is 100.
So if you change the value of MaxIter it should be a number different from 100.