Plotting data from struct
278 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lizan
el 16 de Sept. de 2014
Comentada: Steven Lord
el 7 de Mzo. de 2018
Hi,
I am plotting data from a struct, for example
plot(someStruct.a, someStruct.c)
The figure I obtain I can hardly see my data points. They are tiny dots of different colours when I write;
plot(someStruct.a, someStruct.c,'o-')
It seems to treat the data one by one and does not plot them all as instructed in code. Any idea how to make MATLAB plot the data above with one type of style, colour and size?
1 comentario
Respuesta aceptada
Michael Haderlein
el 16 de Sept. de 2014
If you have something like you suggest to have:
someStruct.a=1:10;
someStruct.c=rand(1,10);
plot(someStruct.a,someStruct.c)
then it's just working. However, I guess you have something different. Is someStruct an array and a,c just one scalar? I mean something like this:
someStruct(1).a=1;
someStruct(2).a=2;
someStruct(3).a=3;
someStruct(1).c=1;
someStruct(2).c=2;
someStruct(3).c=5;
plot(someStruct.a,someStruct.c)
There you can't see a lot. In this case, the solution is simply
plot([someStruct.a],[someStruct.c])
3 comentarios
Hasti Yavari
el 7 de Mzo. de 2018
This was the answer to my question too. However I don't understand the solution. Why does this allow me to plot the struct and not just the dot?
Steven Lord
el 7 de Mzo. de 2018
In this case when you type:
plot(someStruct.a, someStruct.c)
you're passing six inputs into plot, not two. You can see this more clearly by displaying the size of each input to a function. [In this case, displayAllInputs takes the place of plot.]
fh = @(x) fprintf('Input argument is of size %s.\n', mat2str(size(x)));
displayAllInputs = @(varargin) cellfun(fh, varargin);
Now let's build the non-scalar struct array.
someStruct(1).a=1;
someStruct(2).a=2;
someStruct(3).a=3;
someStruct(1).c=1;
someStruct(2).c=2;
someStruct(3).c=5;
When you call it without the square brackets, you'll see that displayAllInputs is called with six inputs.
displayAllInputs(someStruct.a, someStruct.c)
When you call it with the square brackets, you'll see that displayAllInputs is called with two inputs.
displayAllInputs([someStruct.a], [someStruct.c])
Más respuestas (1)
Iain
el 16 de Sept. de 2014
plot(someStruct.a, someStruct.c,'k-o')
That's the answer to the question you're asking.
I suspect that ACTUALLY, you want this:
plot(someStruct.a', someStruct.c','k-o')
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!