Call out specific data columns to use in function.

49 次查看(过去 30 天)
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

回答(1 个)

Star Strider
Star Strider 2020-10-7
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 个评论
Slane Haltine
Slane Haltine 2020-10-8
编辑:Slane Haltine 2020-10-8
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 2020-10-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by