Issue with Setting Position of Uitable in MATLAB - matlab

I am trying to create a uitable in matlab. Consider the following simple example:
f = figure;
data = rand(3);
colnames = {'X-Data', 'Y-Data', 'Z-Data'};
t = uitable(f, 'Data', data, 'ColumnName', colnames, ...
'Position', [20 20 260 100]);
Next, I am trying toset the width and height of the uitable to match the size of the enclosing rectangle:
t.Position(3) = t.Extent(3);
t.Position(4) = t.Extent(4);
However I get the following error:
>> t.Position(3) = t.Extent(3);
t.Position(4) = t.Extent(4);
Attempt to reference field of non-structure array.
When I try to view what t is, I get:
>> t
t =
2.1030e+03
I don't know what this result means! I am a little confused as this is the first time I am working with uitable and I am very new to MATLAB too.
Any help would be appreciated. Thanks!

Per the comments, transposing my comment above into an answer.
For the example code to function properly you will need MATlAB R2014b or newer. Per the release notes for MATLAB R2014b, graphics handles are now objects and not doubles, bringing graphics objects in line with the rest of MATLAB's objects. One benefit to this is the user is now able to utilize the dot notation to address and set the properties of their graphics objects. This is a change from older versions where graphics handles were stored as a numeric ID pointing to the relevant graphics object, requiring the user to use get and set to access and modify the graphics object properties.
To resolve your issue, you just need to modify the dot notation usage to get or set where appropriate. Or upgrade MATLAB :)
For example,
t.Position(3) = t.Extent(3);
t.Position(4) = t.Extent(4);
becomes:
tableextent = get(t,'Extent');
oldposition = get(t,'Position');
newposition = [oldposition(1) oldposition(2) tableextent(3) tableextent(4)];
set(t, 'Position', newposition);

Related

duplicate axes in a figure

Sorry for a somewhat simple question. I am trying to generate a figure which displays the same animation but in different subplots. I'm starting simple and primarily focusing on duplicating the plot first.
I was originally thinking of attaching the handle for the subplot to the other plots
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
a1{2} = a1{1};
a1{2}.Position = [.3,.2,.2,.2];
a1{3} = a1{1};
a1{3}.Position = [.6,.2,.2,.2];
obj = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
But this just moves the existing plot around as opposed to duplicating it. (on account that I'm still referring to the same object even though it has different names)
I next thought of just recreating the same setup 3 times and then update the animation, looping through the three, but this feels inefficient and computationally intensive.
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
a1{2} = axes('Position',[.3,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
a1{3} = axes('Position',[.6,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
obj{1} = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
obj{2} = patch('Parent',a1{2},'XData',[1,3,1],'YData',[1,1,3]);
obj{3} = patch('Parent',a1{3},'XData',[1,3,1],'YData',[1,1,3]);
Is there a way to call 1 subplot, update that 1 subplot but have it propagate to other subplots?
It really depends on what you want to do in the end, how complex the animations are, and if you can prepare your plots in advance.
First, if there are only a few objects, you could use the linkprop function to link graphics objects' properties:
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
obj = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
a1{2} = copyobj(a1{1}, afig);
a1{2}.Position = [.3,.2,.2,.2];
a1{3} = copyobj(a1{1}, afig);
a1{3}.Position = [.6,.2,.2,.2];
linked_objects = [ a1{1}.Children, a1{2}.Children, a1{3}.Children];
property_names = {'XData', 'YData', 'ZData'};
hlink = linkprop(linked_objects, property_names);
for ii = 1:10
obj.XData(1) = ii;
drawnow
pause(0.01)
end
Here, we first create the base plot, then we copy the axes (note that children objects get copied, too, but not callbacks and other properties, see copyboy). We then link properties we may want to change during the animation (note that you could also link the axes' view properties), and then change them in a loop.
Another approach would be to change the object's properties in a main axes in every loop iteration, and copy the main axes' children to the other axes afterwards. This approach may be more costly – as a lot of objects get copied and rendered – but on the other hand, individual properties do not have to be tracked. Here is an example:
afig = figure;
a1{1} = axes('Position',[.01,.2,.2,.2], 'color','none','Xlim',[-10,10],'Ylim',[-10,10]);
obj = patch('Parent',a1{1},'XData',[1,3,1],'YData',[1,1,3]);
a1{2} = copyobj(a1{1}, afig);
a1{2}.Position = [.3,.2,.2,.2];
a1{3} = copyobj(a1{1}, afig);
a1{3}.Position = [.6,.2,.2,.2];
for ii = 1:10
obj.XData(1) = ii;
delete(a1{2}.Children);
delete(a1{3}.Children);
copyobj(a1{1}.Children, a1{2});
copyobj(a1{1}.Children, a1{3});
drawnow
pause(0.01)
end
Finally, it could be an option to use getframe to just capture the rendered image and display it in the copy axes.

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?

Error using legappend()

I am writing a function that iterates through a loop and adds entries to a plot. When I try to use legappend(), though, I get the error below. I am passing it a string variable.
Error using legend>process_inputs (line 526)
Invalid argument. Type 'help legend' for more information.
Error in legend>make_legend (line 303)
[orient,location,position,children,listen,strings,propargs] =
process_inputs(ha,argin); %#ok
Error in legend (line 257)
[~,msg] = make_legend(ha,args(arg:end),version);
Error in legappend (line 74)
[legend_h,object_h,plot_h,text_strings] = legend(h,allDatah,str);
Here is a minimal example, taken from the MATLAB site
% Some data and old models:
x = (1:10)';
y = [x-5+x.^1.05 x-2 x-3 x-4 x-5];
% Plot the data and old models:
figure
plot(x,y(:,1),'mo','markersize',10);
hold on;
plot(x,y(:,2),'r');
plot(x,y(:,3),'b');
plot(x,y(:,4),'k');
plot(x,y(:,5),'kp');
box off
axis([1 10 -5 20])
legend('data','model 1','model 2','model 3','model 4','location','northwest')
legend boxoff
myNewModel = x - 5.5 + x.^1.1;
plot(x,myNewModel,'m','linewidth',2);
legappend('my new amazing model!')
As mentioned in my comment, MATLAB's graphics engine was significantly overhauled in R2014b. While it brought a plethora of very welcome changes, like any major code overhaul it broke functionality in the existing code base. Relevant here is implementing legends as their own object class rather than as a cobbled together axes object. Given its July 2014 release date, legappend was likely created using R2014a and the logic in the code assumes that the legend is an axes object. This unfortunately breaks in the new graphics system.
Fortunately, the fix isn't as complex as I was anticipating. If you take a look at the properties of the new legend object, there's no documented property for the linked data. Attempting to set the 'String' property manually also has no effect. However, if you look at the final syntax in the function description ([l,icons,plots,txt] = legend(___)), it seems clear that legend has a way to access the appropriate internal properties. And indeed, if you poke around in the legend source, you'll find the 'PlotChildren' property, which is an array of object handles.
Putting it all together we get something like the following:
function legappend_HG2(newStrings)
% Quick & dirty fork of legappend specific to MATLAB versions >= R2014b
% Only supports appending strings to the existing legend handle
% Assumes only one legend is in the current figure
% Add multiple strings by passing it a 1D cell array of strings
% Find our legend object
h = findobj(gcf, 'Type', 'legend');
if ~isempty(h)
% Get existing array of legend strings and append our new strings to it
oldstr = h.String;
if ischar(newStrings)
% Input string is a character array, assume it's a single string and
% dump into a cell
newStrings = {newStrings};
end
newstr = [oldstr newStrings];
% Get line object handles
ploth = flipud(get(gca, 'Children'));
% Update legend with line object handles & new string array
h.PlotChildren = ploth;
h.String = newstr;
end
end
Swapping legappend for legappend_HG2 in the above MWE we get the desired result:
Was getting wrong result in R2016b for exkaza's solution. Hence changed the code in a line a little bit.
function legappend_HG2(newStrings)
% Quick & dirty fork of legappend specific to MATLAB versions >= R2016b
% Only supports appending strings to the existing legend handle
% Assumes only one legend is in the current figure
% Add multiple strings by passing it a 1D cell array of strings
% Find our legend object
h = findobj(gcf, 'Type', 'legend');
if ~isempty(h)
% Get existing array of legend strings and append our new strings to it
oldstr = h.String;
%disp(oldstr);
if ischar(newStrings)
% Input string is a character array, assume it's a single string and
% dump into a cell
newStrings = {newStrings};
end
newstr = [oldstr newStrings];
%disp(newstr);
% Get line object handles
ploth = flipud(get(gca, 'Children'));
% Update legend with line object handles & new string array
%h.PlotChildren = ploth;
h.PlotChildren = [h.PlotChildren; ploth];
h.String = newstr;
end
end

How to access Matlab's handle object's sub-property: 'allowedStyles'

I would like to progmatically access the 'sub-property' of a lineseries object's 'MarkerFaceColor' property called 'allowedStyles'. This 'sub-property' can be seen in Matlab's inspector, (inspect(handle)) by expanding the 'MarkerFaceColor' property row.
I would like to do something like the following or get the equivalent of such a command.
allowedstyles = get(hh,'MarkerFaceColorAllowStyles');
Screen shot of Matlab's Inspect window indicating information I seek.
https://drive.google.com/file/d/0B0n19kODkRpSRmJKbkQxakhBRG8/edit?usp=sharing
update:
For completeness my final solution for accessing this information via a cellstr was to write the following function. Thanks to Hoki.
FYI, this information (allowed styles) is useful for a GUI when you want to offer user choices for a property such as MarkerFaceColor, where you don't know the type of graphics object they are modifying. I populate a listbox with these 'allowedStyles' along with an option to set a colour. Mesh plot 'MarkerFaceColor' allows styles {'none','auto','flat'}, while a lineseries plot has {'none','auto'}.
function out = getAllowedStyles(hh,tag)
% hh - handle returned from plot, surf, mesh, patch, etc
% tag - the property i.e. 'FaceColor', 'EdgeColor', etc
out = [];
try
aa = java(handle(hh(1)));
bb = eval(sprintf('aa.get%s.getAllowedStyles;',tag));
bb = char(bb.toString);
bb(1) = []; bb(end) = [];
out = strtrim(strsplit(bb,','));
end
end
I think it is indeed ReadOnly (or at least I couldn't find the correct way to set the property, but it is definitely readable.
You need first to access the handle of the underlying Java object, then call the method which query the property:
h = plot([0 1]) ; %// This return the MATLAB handle of the lineseries
hl = java(handle(h)) ; %// this return the JAVA handle of the lineseries
allowedstyles = hl.getMarkerFaceColor.getAllowedStyles ; %// this return your property :)
Note that this property is actually an integer index. Your inspect windows translate it to a string saying [none,auto] while in my configuration even the inspect windows only shows 1.
If you want the exact string translation of other values than one, you can call only the parent method:
hl.getMarkerFaceColor
This will display the allowed style in plain text in your console window.
ans =
com.mathworks.hg.types.HGMeshColor#28ba43dd[style=none,allowedStyles=[none, auto],red=0.0,green=0.0,blue=0.0,alpha=0.0]
If you insist on getting this property as a string progamatically, then you can translate the above using the toString method.
S = char( hl.getMarkerFaceColor.toString )
S =
com.mathworks.hg.types.HGMeshColor#1ef346e8[style=none,allowedStyles=[none, auto],red=0.0,green=0.0,blue=0.0,alpha=0.0]
then parse the result.

How to write this in an elegant way (cell arrays and structs in MATLAB)

I would like to plot connected points in MATLAB.
My connected points come from connecting objects of "stats", where each "stat" comes from a BW regionprops struct.
The code I have written works, but it suffers from a lot of "ugliness", which I couldnt fix even after trying various ways.
function plot_line( line )
a = cell2mat(line);
b = {a.Centroid};
matx = {};
maty = {};
for i = 1:size(b,2)
matx{end+1} = b{i}(1);
maty{end+1} = b{i}(2);
end
plot ( cell2mat(matx), cell2mat(maty) );
end
Can you help me make this code nicer? It's not critical, as my code works fine and as the lines are short (<100 points) the performance is not an issue.
It is just that it would be really nice to know how this tiny function should be written in the proper way, without for loops and 3 calls of cell2mat.
In my example:
line is a <1xn cell>,
line{1} has a property 'Centroid' and
line{i}.Centroid(1) are the x coordinates and
line{i}.Centroid(2) are the y coordinates.
Actually, all I need is plotting line{i}.Centroid(1), line{i}.Centroid(2) for i = 1:size(line,2), but I don't know how.
Instead of creating a cell array b, you can create a numerical array directly, by catenating using CAT:
tmp = cat(1,line{:});
coordinates = cat(1,tmp.Centroid);
plot(coordinates(:,1),coordinates(:,2))
EDIT
If you want to keep it really short (i.e. even shorter than #Amro's solution you can use CELLFUN like this in order get a one-liner:
plot(cellfun(#(x)x.Centroid(1),line),cellfun(#(x)x.Centroid(2),line))
Example:
line = repmat({struct('Centroid',[1 2])},1,5); %# similar to the data you have
%# extract x/y coordinates
x = cellfun(#(s)s.Centroid(1),line)
y = cellfun(#(s)s.Centroid(2),line)
%# plot
plot(x,y)
You could also do it as:
xy = cell2mat(cellfun(#(s)s.Centroid, line, 'UniformOutput',false)');
plot(xy(:,1),xy(:,2))