Read and write text to matrix and write to Excel
6 views (last 30 days)
Show older comments
bah327hp bah327hp
on 22 Jun 2017
Answered: Walter Roberson
on 22 Jun 2017
Hello. Here's what I am trying to do.
1. Read in data from a sheet called 'StimuliInExcel' in an Excel spreadsheet. Rows 1, 6, 7, and 8 are numbers, row 2 is blank, and rows 3, 4, and 5 are text. Assign it to Matrix.
2. Randomly shuffle the rows.
3. Go through each line. If it is an even-numbered row, print 'PalmerTrialProc' in Matrix(i,4). If it is an odd-numbered line, print 'WilliamsTrialProc' in Matrix(i,4).
4. Write this new information to a sheet in the Excel document called 'UpdatedList'.
It is not printing 'PalmerTrialProc' or 'WilliamsTrialProc' in Matrix(i,4) as it iterates through the loop. I am not sure how to make it do that. Here is my current code:
Matrix=xlsread('StimuliInExcel.xlsx', 'CompleteList')
Matrix=Matrix(randperm(124),:)
Matrix
for i = 1:124
if mod(i,2)==0
Matrix(i,4)=fprintf('%s','PalmerTrialProc')
else
Matrix(i,4)=fprintf('%s','WilliamsTrialProc')
end
end
xlswrite('StimuliInExcel.xlsx',Matrix,'UpdatedList')
I think this issue has something to do with NaN appearing in my Matrix.
Is there a way to write text to these NaN cells? Is the issue with how they are read?
If I type Matrix(i,4)='PalmerTrialProc' in the IF statement like so:
Matrix=xlsread('StimuliInExcel.xlsx', 'CompleteList')
Matrix=Matrix(randperm(124),:)
Matrix
for i = 1:124
if mod(i,2)==0
Matrix(i,4)='PalmerTrialProc'
else
Matrix(i,4)='WilliamsTrialProc'
end
end
xlswrite('StimuliInExcel.xlsx',Matrix,'UpdatedList')
Then I get this message:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
If I type this:
Matrix=xlsread('StimuliInExcel.xlsx', 'CompleteList')
Matrix=Matrix(randperm(124),:)
Matrix
for i = 1:124
if mod(i,2)==0
xlswrite('StimuliInExcel.xlsx',Matrix(i,4),'PalmerProc','UpdatedList')
else
xlswrite('StimuliInExcel.xlsx',Matrix(i,4), 'WilliamsProc','UpdatedList')
end
end
xlswrite('StimuliInExcel.xlsx',Matrix,'UpdatedList')
I get the following error, which I think means I need to consider how I convert i to a corresponding letter of the alphabet, but I'd rather not go down that road:
The specified data range is invalid or too large to write to the specified file format. Try writing to an XLSX file and use Excel A1 notation for the range argument, for example, ‘A1:D4’.
Thank you for any help you can provide.
0 Comments
Accepted Answer
Walter Roberson
on 22 Jun 2017
You are passing the constant string 'UpdatedList' in the position where an Excel range would need to go.
When you use
Matrix=xlsread('StimuliInExcel.xlsx', 'CompleteList')
then the output in Matrix is going to be only numeric, and any all-text headers or all-text leading columns would be removed.
In your loop you do
Matrix(i,4)=fprintf('%s','PalmerTrialProc')
The output of fprintf() is the count of the number of characters that were produced and displayed. You are then putting that (numeric) count into Matrix.
"Go through each line. If it is an even-numbered row, print 'PalmerTrialProc' in Matrix(i,4). If it is an odd-numbered line, print 'WilliamsTrialProc' in Matrix(i,4)."
That would require that you convert Matrix into a cell array.
What you should probably be doing is using
[~, ~, Matrix] = xlsread('StimuliInExcel.xlsx', 'CompleteList')
and then working with the "raw" data in Matrix, which will be a cell array with a mix of strings and numeric values.
0 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!