Clear Filters
Clear Filters

Call out specific data columns to use in function.

24 views (last 30 days)
I am just starting to learn how to use functions. I am trying to create a function that has multiple outputs and uses two vectors with data. The data in from an imported excel fine named file.xlsx. I am trying to create a function that will call on two columns (1&2 and separately, 4&10). My function will eventually plot the means in a bar graph (and include text at the top) so this is what I have so far (VERY simple) but I am not sure how to write the input in order to call out specific columns. I am assuming I will call out the specific vectors, from the data, when I actually use the function as apposed to calling out specific vectors in the function itself. any help would be appreciated.
function [m,BG] = av(x)
%
m = mean(x)
BG = bar(m)
end

Answers (1)

Star Strider
Star Strider on 7 Oct 2020
I would do something like this:
function [m,BG] = av(x)
xsel = x(:,[4 10]);
m = mean(xsel);
figure
BG = bar(m);
end
save it as av.m on your MATLAB user path, then call it as:
D = readmatrix('file.xlsx');
[m,BG] = av(D);
Note that ‘BG’ will be a (1x2) bar array. If you want to re-create it later:
figure
bar(BG.XData, BG.YData)
.
  12 Comments
Slane Haltine
Slane Haltine on 8 Oct 2020
Edited: Slane Haltine on 8 Oct 2020
I believe I was able to make it work, but not entirely through the function. I called out the specific columns and assigned variables, then ran the function using those variables. Thank you for all of your patients/help with this.
function [m,BG] = av(x)
%
m = mean(x)
figure(1); hold on
BG = bar(m)
end
%function to find mean and plot into bar graph
C1 = xlsread('MetabolicSummaryData.xlsx', 'MetricSurface','B3:M12') %create MetabolicData.m to extract data
MetabolicData %run MetabolicData.m
x = C1(:,[1 2]);% - Condition 1 and 2
av(x)
y1 = C1(:,4);% - Condition 4 and 10
y2 = C1(:,12);
y = [y1 y2];
av(y)
Star Strider
Star Strider on 8 Oct 2020
That appears to be correct.
However, while was off doing other things, intending to come back here in a few minutes, the file seems to have disappeared.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!