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
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!