Read and write text to matrix and write to Excel
Show older comments
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.
Accepted Answer
More Answers (0)
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!