Matlab draw line with plot and dynamic values - matlab

I am trying to draw line with plot in matlab app designer. But i dont know how to create line. I am just redrawing point on new coordinates.
This is my code (functions runs each second) (format of plot is plot(PlotUI,X,Y))
function function2(app)
app.timeCounter = app.timeCounter + 1;
plot(app.UIAxes,app.timeCounter,app.newValDblPublic);
end
I will be thankful for any help.

At the moment you are just plotting the current set of values, if you want to plot the historic values too, you need to keep them in an array and plot the entire array.
%When the GUI is first created, start with one value in the array
app.time_values = [0];
app.y_values = [0];
%Inside your function
function function2(app)
app.time_values(end+1) = app.time_values(end)+1; % Add a new value to the array, 1 greater than the last value
app.y_values(end+1) = app.newValDblPublic;
plot(app.UIAxes,app.time_values,app.y_values);
end

Related

Plotting brownian motion matlab

First of all, I just want to say that I'm not that used to using matlab, but I need for an assignment, I'm supposed to create a "brownian movement". My code is currently looking like this:
clf
hold on
prompt = 'Ge ett input';
size = input(prompt) ;
numParticles = input('Ange antal partiklar');
axis([-size size -size size]);
Part = [];
color = 'brkgmyco';
for i = drange(1:numParticles)
Part = [Part [0;0]];
end
for i = drange(1:200)
dxdy = randn(2,numParticles);
k = Part
Part = Part + dxdy;
My concern is how to print, I would even want like a small delay on every print, so you really can see what's happening for the assignment, is this possible to achieve from the code I've written for now or should anything be changed? Thanks in advance!
Here are some basic problems with your code, regardless of what you are trying to do:
You use size as a variable name. Doing so overrides MATLAB's function size.
The function zeros creates an array initialized by zeros, no need for a loop for that.
Instead of calculating randn for 200 times in a loop, you can do it once, with dxdy = randn(2,numParticles,200) and then simply refer to dxdy(:,:,i) within the loop.
The same holds for summation. Instead of summing within a loop to get the cumulative sum, use cumsum like Part = cumsum(randn(2,numParticles,200),3); and then refer to Part(:,:,i), within the loop.
Now to your task. You said you want to know how to print, but I believe you want to plot because you use some commands like axis, clf and hold, that refer to graphic objects. However, you never really do plot anything.
The basic and general function for plotting in 2D is plot, but there are many other more specific functions. One of them is scatter, and it has a sister function gscatter, that takes triples of x, y and groupand plot each (x(k),y(k)) colored by their group(k).
This code plots the particles on an axes, and animate their movement:
prompt = 'Ge ett input';
scope = input(prompt) ;
numParticles = input('Ange antal partiklar');
N = 500;
Part = cumsum(randn(2,numParticles,N)*scope/100,3);
h = gscatter(Part(1,:,1),Part(2,:,1),1:numParticles);
axis([-scope scope -scope scope]);
legend off
for k = 2:N
for p = 1:numParticles
h(p).XData = Part(1,p,k);
h(p).YData = Part(2,p,k);
end
drawnow
end
Is this what you look for?

missing graphs in subplot in matlab

I have such a code;
figure;
for a = 1:length(weekM')
tday_cell = day_cell(week_cell == weekM(a));
for b = 1:length(days)
subplot(length(weekM'), 7, b);
if strcmp(first_entranceM{a, b}, '0')
plot(peaks);
l = [l; 0];
else
tms_cell = ms_cell(week_cell == weekM(a));
ttms_cell = tms_cell(strcmp(tday_cell,days{b}));
l = [l; ttms_cell];
plot(ttms_cell);
end
end
end
I want to plot graphs with subplot function as subplot(3,7,[1:2:3...]). That is, I want to plot graph which has 3 lines, and 7 graphs on each line.
But in my case, it only shows the last line on the first line, and I can't see the remaining graphs.
I'm sure that the data to plot previous graphs are not being lost, but I don't understand why there are missing graphs.
Could you help me to fix this problem?
Your line with the subplot-statement needs to be changed. The first argument is correct. The second argument represents the number of columns and needs to be set to 7 here, since you have 7 days. The last argument is the index of the addressed subplot and needs to be changed in every iteration.
This index can be generated using the sub2ind-function, which calculates the index from the subscripts. The following line can be used instead of yours:
subplot(length(weekM'),7,sub2ind([7,length(weekM')],b,a));

Dynamically change variable name inside a loop in MATLAB

This script is being used for image processing by multiplying a set of 2000 images with a mask and then summing the values in each frame. These values are entered into a row vector called Intensity.
I am trying to end up with 20 row vectors called intensity1, intesity2...intensity20, is there a straight forward way to change the name of the Intensity row vector upon every loop iteration?
for m=1:20
mask=bigrating(m,m,0);
for n=1:2000
I=sum(sum(imread((sprintf('image%05d.tif',n))).*(mask)));
Intensity(n)=I;
end
save('filepath','Intensity')
end
Because you wanted dynamically named intensity1, intensity2,....intensity20 etc, the following should work for you:
for m = 1:20
mask = bigrating(m,m,0)
for n = 1:2000
I=sum(sum(imread((sprintf('image%05d.tif',n))).*(mask)));
eval(['intensity' num2str(m) ' = I'])
end
save('filepath', ['intensity' num2str(m)])
end

Customizing cursor in Matlab

In Matlab 2012a I have generated a figure from a previous code that is SSI as a function of age.
I want to customize datatip by updating my own function instead of the default one. I know how to change x and y and now I have Age and SSI for them. However, I have another piece of information -subjectID- which I want to add to the display text.
By clicking on each point, I want datatip to show Age, SSI and subject ID of corresponding data point.
This is what I have now:
matlab is a saved work place of my SSI-age.
function output_txt = myupdatefcn(obj,event_obj,...
matlab,labels,SubjectID)
pos = get(event_obj,'Position');
x = pos(1);
y = pos(2);
[~, ~, raw0_0] = xlsread('Data.xlsx','CONTROLS','A2:A106');
raw = [raw0_0];
SubjectID = cell2mat(raw);
output_txt = {['AGE: ',num2str(pos(1),4)],...
['SSI: ',num2str(pos(2),4)],...
['SubjectID: ',SubjectID]};
idx = find(matlab == x,1);
[row,col] = ind2sub(size(matlab),idx);
output_txt{end+1} = cell2mat(labels(row));
Obviously, this is not right. Can somebody please help me out here? Thank you.
If I read your code correctly, I make the following assumptions (which may be incorrect):
* the subjectID is a cell containing a vector of strings
* subjectID is X position of the clicked point
First, a quick digression: getting SubjectID into your plot
I noticed, in your function call, you have SubjectID as one of the input parameters. However, it appears that it would never be used, since the next line that uses it assigns it a value.
As written, this will read from the excel file each and every time that the update function is called. You might wish to move the load-from-excel portion into the same section of code where the data is first loaded. If I assume the SubjectID is text, you can store it in the UserData variable of the timeseries. That would make the following work:
Onward to the answer
So, if you include your SubjectID information in the userdata, when you first plot like so:
% ...not shown: get the ages, SSIs and SubjectIDs ....
plot(ages, SSIs, 'UserData', SubjectIDs); % Store SubjectIDs along with the line...
Then the following should work -- or at least put you on solid ground.
function output_txt = myupdatefcn(obj,event_obj)
pos = get(event_obj,'Position');
x = pos(1);
y = pos(2);
allIDs = get(event_obj.Target,'UserData');
thisSubject = event_obj.UserData{pos(1)};
output_txt = {['AGE: ',num2str(pos(1),4)],...
['SSI: ',num2str(pos(2),4)],...
['SubjectID: ',thisSubject]};
You can probably get rid of the last 3 lines of code, since you know a priori that all 3 values are accessible.
Hope that helps.

Line Matrix code in Matlab

function [fl re]=lines(im_text)
%# Divide text in lines
im_text=clip(im_text);
num_filas=size(im_text,1);
for s=1:num_filas
if sum(im_text(s,:))==0
nm=im_text(1:s-1, :); %# First line matrix
rm=im_text(s:end, :);%# Remain line matrix
fl = clip(nm);
re=clip(rm);
%#result
break
else
fl=im_text;%#Only one line.
re=[ ];
end
end
function img_out=clip(img_in)
[f c]=find(img_in);
img_out=img_in(min(f):max(f),min(c):max(c));%#Crops image
Can anyone please provide with an explanantion of this code and how i can summarise it
and simplify it without changing the function its performs? How i can implement same algorithm without making it a function on its own. adding this to another code.
thanks
The function takes a binary image, then looks for a row in the image with no object (i.e. only black pixels), and splits the image along this line. In addition, it crops off all the empty (i.e. black) area around the objects in the subfunction clip.
You can simplify it to
cutRowIdx = find(all(~im_text,2),1,'first');
topPart = clip(im_text(1:cutRowIdx-1,:));
bottomPart = clip(im_text(cutRowIdx:end,:));
Put clip as a subfunction into your function, or make it into a standalone function and put it into your path.