How do I plot multiple, disparate lines onto one graph showing changes between 2 conditions?

Hello,
I am analyzing balance data, and there are 2 conditions and 5 subjects. I want to plot each subject as a line onto one graph, where condition 1 and condition 2 are on the x-axis, and sway values are on the y-axis (so I can see change between conditions for each subject). I am using a data sheet, and have tried the following syntax:
plot(categorical(date.Condition),date.PosturalSway_Acc_CentroidalFrequency_Coronal__Hz_)
The following figure comes out:
Untitled.png
This is obviously messy. I want individual lines for each subject (not connecting every point). Is there any simple code that can accomplish this?
Thanks,
B

 Accepted Answer

I would suggest a quick loop through each of your subjects. You can keep multiple lines on a single plot by using the hold command.
figure(1)
hold on
for i = 1:...
plot(...)
end

4 Comments

I'm sorry, I am relatively new to Matlab. The logic of your answer is sound, but I am not grasping what to substitue into the "for" section. I know that "i" is index, so would this be my dependent variable (sway frequency in the data table)? and after the 1: ... Would this be "2" in my case since I have two conditions? or would this be a label for my conditions.
Again, my apologies if these are overly basic questions. Thanks for the help.
B
That's a fine thing to ask clarification on. I didn't fill it out much more because I'm not really sure how your data is organized, but I will do my best to help you understand what you're looking for.
Based on how the code is set up the for loop is intended to generate a new line each time it loops. The end bound then needs to be equal to the number of lines you would expect to have in total. You said, 'I want individual lines for each subject,' so the for loop should go from 1:(total number of subjects).
Within the for loop things get a little bit more tricky. From the image I'm going to guess that you have about five subjects, but your data appears to be stored in just two columns. You're going to need to use the for loop index, i, in your indexing of your data to make sure you are only plotting the correct pair of points at a time. If your points are just paired one after the other then you can probably use something like this (you will need to fill out the full variable names).
plot(categorical(date.Con(i*2-1:i*2)),date.Pos(i*2-1:i*2))
>> figure(1);hold on;for i = 1:5;
plot((categorical(date.Condition)(i*2-1:i*2)),
date.PosturalSway_Acc_CentroidalFrequency_Coronal__Hz_(i*2-1:i*2));end
What you said makes sense, I appreciate the thought and explanation. Just to be clear, is this the code you are suggesting? It gives me an Error stating indexing must appear last in an index expression. I assume that means that it does not like the code after the Frequency variable.
B
I'm not sure if that's it or not. A quick way to check these types of things is to grab parts of your command and put them in the command line. If you get the same error then you know what portion is causing the error. It might actually be 'categorical(date.Condition)(i*2-1:i*2)' because you're trying to take the index of the result of categorical() rather than of the date.Condition protion. If this does reproduce the error then move your indexing inside the categorical() command call.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!