so I am trying to read multiple sheets in excel using matlab by this function:
Data_mat=readmatrix('DATA_I.xlsx');
I tried a couple of things using additional info, bu seems not correct.
If you want the output to be a matrix, use that command. On the other hand, if you want to read tables (considering the column name), you can use the readtable command.
I generated a dummy excel file that only contains 2 sheets with some numbers there. If you want to read the matrices, use the readmatrix, or if you want tables, just change that function to readtable. Both functions have the same argument structure.
% Define the file name
FileName = "MatrixesInFile.xlsx";
% Get the sheets in the excel file
Sheets = sheetnames(FileName);
% Generate the variable with the matrices
myMatrices = struct;
% Read the matrices and store it in the struct
for n=1:length(Sheets)
% Get evaluated sheet
eval_sheet = Sheets{n};
% Get the matrix
values = readmatrix(FileName,'Sheet',eval_sheet);
% Create a field in the struct and store the matrix there
myMatrices.(eval_sheet) = values;
end
% Print the variable
myMatrices
Related
I am trying to get some variables and numbers out from an Excel table using Matlab.
The variables below named "diffZ_trial1-4" should be calculated by the difference between two columns (between "start" and "finish"). However I get the error:
Undefined operator '-' for input arguments of type"
'cell'.
And I have read somewhere that it could be related to the fact that I get {} output instead of [] and maybe I need to use cell2mat or convert the output somehow. But I must have done that wrongly, as it did not work!
Question: How can I calculate the difference between two columns below?
clear all, close all
[num,txt,raw] = xlsread('test.xlsx');
start = find(strcmp(raw,'HNO'));
finish = find(strcmp(raw,'End Trial: '));
%%% TIMELINE EACH TRIAL
time_trial1 = raw(start(1):finish(1),8);
time_trial2 = raw(start(2):finish(2),8);
time_trial3 = raw(start(3):finish(3),8);
time_trial4 = raw(start(4):finish(4),8);
%%%MOVEMENT EACH TRIAL
diffZ_trial1 = raw(start(1):finish(1),17)-raw(start(1):finish(1),11);
diffZ_trial2 = raw(start(2):finish(2),17)-raw(start(2):finish(2),11);
diffZ_trial3 = raw(start(3):finish(3),17)-raw(start(3):finish(3),11);
diffZ_trial4 = raw(start(4):finish(4),17)-raw(start(4):finish(4),11);
You are right, raw contains data of all types, including text (http://uk.mathworks.com/help/matlab/ref/xlsread.html#outputarg_raw). You should use num, which is a numeric matrix.
Alternatively, if you have an updated version of Matlab, you can try readtable (https://uk.mathworks.com/help/matlab/ref/readtable.html), which I think is more flexible. It creates a table from an excel file, containing both text and numbers.
I would like to be able to export the Value from each Name in the Workspace using MATLAB.
I have some questions, which then sets the variables in the workspace. I have tried xlswrite but I have been unsuccessful in being able to export the file. I don't think that it would export just the value, which I would like it to do.
% Construct a questdlg with three options
choiceprepq = 'Which animal do you like?';
choiceprep = questdlg('Which animal do you like?', 'Which animal do you like?', 'Dog', 'Cat','Fish');
% Handle response
switch choiceprep
case 'Dog'
disp([choiceprep ' Dog Selected.'])
case 'Cat'
disp([choiceprep ' Cat Selected.'])
case 'Fish'
disp([choiceprep ' Fish Selected.'])
end
alldata= whos('global');
filename='Test.xls';
xlswrite(filename,alldata)
Is there any way to export the question and the value which the user selects from the question boxes as an Excel file? Not the name of the variable, choiceprep, but its value.
An example in this case would be:
First of all, unless you actually declare choiceprepq and choiceprep as global variables, using whos('global') won't give you those two. It's not necessary here anyways, because you already know the variables you want.
Also, you're using questdlgwrong, you need to specify your default answer, so it should be like this:
% syntax : button = questdlg('qstring','title', default)
choiceprepq = 'Which animal do you like?';
choiceprep = questdlg(choiceprepq, choiceprepq, 'Dog', 'Cat', 'Fish', 'Dog');
When you do xlswrite, it actually only writes the values held by the variables. In your case, you can easily store the question and answer into an xls file by this:
filename = 'Test.xls';
xlswrite(filename, {choiceprepq, choiceprep})
The curly braces turn the two variables into a 1x2 cell array whose values can be easily written into xls file by xlswrite.
xlswrite does not seem to work properly on Mac OS. Here's a MATLAB file exchange submission that's supposed to help : http://www.mathworks.com/matlabcentral/fileexchange/38591-xlwrite--generate-xls-x--files-without-excel-on-mac-linux-win
I would like to read an excel file (xlsread) but I don't want to put manually the string every time but instead I want to xlsread the name of the file that is contained in an array.
For example, my array B is:
B =
'john.xlsx'
'mais.xlsx'
'car.xlsx'
Then I would like to read the excel WITH THE NAME that is inside the first element, that means: "john.xlsx"
How can I do this?
data = xlsread(B{1});
Or, if you want to read all of them:
for i=1:length(B)
data(i).nums = xlsread(B{i});
end
Assuming, of course, your B is a cell array. If it's not, it can't exist the way you described it. If all strings have the same length (then it would be possible) or padding with spaces, you can split the char array into a cell array using
B = mat2cell(B,ones(size(B,1),1),size(B,2));
Strings of different lengths would have to be inside a cell array, which you can access elements via the curly brackets {}. So, you can call xlsread on the first element this way:
names{1} = 'john.xlsx';
names{2} = 'mais.xlsx';
names{3} = 'car.xlsx';
num = xlsread(names{1});
In my code I'm trying to use load with entries from a cell, but it is not working. The portion of my code below produces a 3 dimensional array of strings. The strings represent the paths to file names.
for i = 1:Something
for j = 1:Something Different
for k = 1: Yet Something Something Different
DataPath{j,k,i} = 'F:\blah\blah\blah\fileijk %file changes based on i,j,and k
end
end
end
In the next part of the code I want to use load to open the files using the path names defined in the code above. I do this using the code below.
Dummy = DataPath{l,(k-1)*TSRRange+m};
Data = load(Dummy);
The idea is for Dummy to take the string content out of DataPath so I can use it in load. By doing this I thought that Dummy would be defined as a string and not a cell, but this is not the case. How do I pull the string out of DataPath so I can use it with load? Thanks.
I have to load the data this way because the data is located in multiple folders. I can post more of the code if needed, but it is complex.
Dummy is a cell because you assigned a 3D cell array but are accessing a 2D cell with Dummy = Datapath{1,(k-1)*TSRRange+m}
I don't believe that you can expect to access all cell elements I this way. Instead, use three indices just as you did when creating it.
I have imported a matrix X filled with data, and its according headers for each column into MATLAB. Now the problem is how can I rename each column of X by its according name in the header cell.I would like to do this in a loop.
Would anyone tell me how can I loop a rename programme in this situation?
I suggest creating a structure out of the data, rather than individual variables. Even with a large number of columns, this will not clutter the workspace, nor will it overwrite variables already in the workspace in the case of a name collision. It will keep all the data from the spreadsheet together, and still allowing access to it by column name. To easily create a structure from a cell array of column names and a matrix of data, use cell2struct:
>> colnames = {'odds','evens'};
>> data = [1 2;3 4;5 6];
>> spreadsheet_structure = cell2struct(num2cell(data,1), colnames, 2)
spreadsheet_structure =
odds: [3x1 double]
evens: [3x1 double]
(num2cell(M,1) creates a cell array in which each cell is a column from matrix M)
Loop through the header columns and use eval to create variables with names contained as strings in your matrix "header":
[X,header,~] = xlsread('eaef21.xls',1,'A1:AY541');
for H = 1:size(header,2)
eval([header(1,H), " = X(:,", H, ");"]);
end
Also it is often very useful to replace the eval above with disp until you are satisfied that it is working as you want it to. Using disp will help you understand what is going on as well.