Call out specific data columns to use in function.
Mostrar comentarios más antiguos
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
Respuestas (1)
Star Strider
el 7 de Oct. de 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 comentarios
Slane Haltine
el 7 de Oct. de 2020
Editada: Slane Haltine
el 7 de Oct. de 2020
Star Strider
el 7 de Oct. de 2020
Editada: Star Strider
el 7 de Oct. de 2020
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.
Slane Haltine
el 7 de Oct. de 2020
Editada: Slane Haltine
el 7 de Oct. de 2020
Star Strider
el 7 de Oct. de 2020
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.
Slane Haltine
el 7 de Oct. de 2020
Star Strider
el 7 de Oct. de 2020
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.
Slane Haltine
el 7 de Oct. de 2020
Star Strider
el 7 de Oct. de 2020
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.
Slane Haltine
el 7 de Oct. de 2020
Star Strider
el 7 de Oct. de 2020
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.
Slane Haltine
el 8 de Oct. de 2020
Editada: Slane Haltine
el 8 de Oct. de 2020
Star Strider
el 8 de Oct. de 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.
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!