1

I have this short code below but I am trying to import 5 excel files, names 101, 102, 103, 104 and 105.xlsx into matlab's workspace as separate cells so i can later make figures by calling on a column in each cell array.

They have the same number of columns (BS:BX) 6 columns is all I need but will have various rows. I'm new to MATLAB so maybe I am missing something easy here, it doesn't like the line "filename = (k,'%dtrf.xlsx');". "Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters."

clear all

close all

mkdir myfolder

dir myfolder

for k = 101 : 105

filename = (k,'*.xlsx');

data = readmatrix(strcat(filename),'Range','BS:BX');

end
1
  • Hi and welcom to SO. The way you are trying to generate a filename or in other word string variable is no right, try something like filename=sprintf('%d.xlsx',k) Commented Mar 12, 2021 at 6:05

1 Answer 1

1

Based on the error message you provided and the format (k,'%dtrf.xlsx'), your loop should probably be something like:

data=cell([5 1]);
for k=101:105
  filename=sprintf('%dtrf.xlsx',k);
  data{k-100} = readmatrix(filename,'Range','BS:BX');
end

where BS, BX are assumed to be valid column names in your .xlsx files.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thank you for your comment, I forgot to add that the exact name of my xlsx files are "101trf.xlsx" hence why trf is in the for loop. It ran without an error but my workspace doesn't have any data now. It has data which is a 5x1 cell, filename which is 105trf.xlsx and k which is 105.
You can type data{1} to see the data that were read from the first file, data{2} for the second file, and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.