Call out specific data columns to use in function.

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 个)

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 个评论

I am unable to get that solution to plot a bar graph. Also, that command reads the entire excel file and assigns it as 'D'. I only want to call out specific columns. The second command [m,BG] = av(D); gives me an error to "check for mismatched delimiters". It also seems to create outputs from the average of (D), which is the entire matrix of data. Sorry if I am misinterpretting, I am very new to MATLAB.
I have no idea where the error could be coming from.
to read only specific rows and columns from a spreadsheet, see the 'Range' name-value pair documentation in Text and Spreasheet Files (there is no way to link to it directly).
EDIT — (07 Oct 2020 at 17:41)
When I run this example code:
x = ones(5,1)*(1:15);
xsel = x(:,[4 10])
m = mean(xsel);
figure
BG = bar(m);
I get the expected results.
Maybe I can clarify what I am attempting to create
% Write a function that uses the following inputs:
% - Two column vectors with data
%
% - Function needs to contain the following operations:
% - calculating the mean of both columns
% - plotting the means in a bar plot
% - printing a text on top of the bar plot that says:
% "Mean of left condition is XXX times mean of right condition"
% XXX should be the ratio of mean of left condition versus mean of right condition.
I then load the excel spreadsheet and need to apply the created function to columns 1&2 then 4&10.
I am having trouble creating a function to calculate the mean of both columns.
I don’t see the problem. Run my example code, since it works, and will do what you want.
Also, I didn’t realise that this is homework. Our policy here on Answers is to only offer hints for homework problems, not complete code.
I appologise, I was unaware of the Policy. I hope you can still provide me with some help (instead of entire solution). I am writing the function to calculate the mean and then will assign data1 to columns 1&2 and then use the function on data1 then to do the same to columns 4&10. If this method would work, I am having trouble specifying the column from the excel sheet. I am able to do this for any other matrix in the workspace but am having trouble with the data from the excel sheet.
You can either read in the entire Excel file and then choose the columns, similarly to what I did in my example code, or you can use the 'Range' name-value pair in readmatris or other function to selectively read those column ranges. Reading non-contiguous columns might require separate calls to readmatrix. I woul just read in the entire file and then choose the columns in the calling script, then pass the selected columns to your function. It would likely best to horizontally concatenate them and pass the concatenated matrix as a single variable.
Okay, what you did in your code looks correct. That is what I was attempting to do but I was using data1 instead of xsel as a variable. This is where I was unable to specify - x(:,[4 10]) - ( from the second line in your code) but as I mentioned it works well with a variable in the workspace (for example the x variable created in your code, or any other variable in my workspace) however, I am having trouble specifying those columns from the excel sheet data.
I am not certain what ‘I am having trouble specifying those columns from the excel sheet data’ means. Are you having problems with the 'Range' values or are you reading in the entire file and are having trouble extractin the columns?
Selecting the columns from the matrix of the entire file is straightforward, and I already gave you an example of that.
I don’t have the file, so I can only direct you back to the documentation for help with raeding the specific columns. If you are reading non-contiguous columns (specifically 4 and 10), that will likely require one readmatrix call for each column, unless you read in 4 through 10, and then select the first and last columns. That would only require one readmatrix call, at least for those columns. I have no idea what your constraints are for this assignment, so you need to do whatever is best.
There are no constraints, I should clarify that this is an assignment to familiarize me with the program because our research labs next data collection will be in the form on an excel file and I do not have experience with this.
Hopefully this will clarify my problem, and hopefully I am interpretting this correctly-
x = ones(5,1)*(1:15);
xsel = x(:,[4 10])
The first line creates a matrix and the second line calls on every row of column 4&10. If this is correct, this makes sense. However, my issue is when I try the same approach to call on the same vectors from the data from the excel file.
That’s correct. I created the colums as I did to make the result less ambiguous.
The result of reading the Excel file should simply be a matrix, so the same approach should work on it.
If this is not graded homework and simply part of your research, I can offer more help. If you want to attach the Excel file, I can likely offer some guidance on reading it and extracting data from it. It would likely still be best to read the entire file and then select the columns, rather than using several readmatrix calls to read specific columns.
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)
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by