Import an Excel File
Show older comments
Hi,
I have given an Excel table. This Excel table I have imported with "Import Data" and saved as a cell array as "generate function". Now I want to asign the content of the imported file to a variable. Can I do that with: imp = importfile_1(file path of the created import file);
The problem is that it doesn't work atm. The entries in the variable are not equal to the excel entries.
At the end I want to have a two dimensional array with the entries of the Excel file.
I hope one of you can help me.
4 Comments
Cedric
on 24 Sep 2017
Can you attach a slice of the original file, and explain how you would like the content to be "split" (according to your comment to Azzi's answer) ultimately?
The problem is that none of us seems to understand what you are trying to achieve overall. So we should start from the beginning: if you are trying to read the content of a file and extract part of this content, could you attach this file to your post? The is a staple icon for this purpose in the editor. Then tell us what it is that you want to extract, and for what purpose.
mb12345
on 25 Sep 2017
Accepted Answer
More Answers (3)
Azzi Abdelmalek
on 24 Sep 2017
0 votes
Use xlsread function
3 Comments
mb12345
on 24 Sep 2017
Image Analyst
on 24 Sep 2017
To format the Excel worksheet before importing it into MATLAB, you need to use ActiveX. That will let you do things like format the font and how many decimal places are showing, etc.
mb12345
on 24 Sep 2017
Image Analyst
on 24 Sep 2017
Looks like you forgot the extension. Try:
fullFileName = 'C:\Users\UserX\Desktop\filetoopen.xlsx'
[~, ~, imp] = xlsread(fullFileName);
Or if the data has the same data type in every column, you could use readtable():
imp = readtable(fullFileName)
15 Comments
mb12345
on 24 Sep 2017
mb12345
on 24 Sep 2017
Image Analyst
on 24 Sep 2017
Well obviously you have to change "filetoopen" with the actual filename!
You're the one who gave that pseudocode name to it originally, not me, so I assumed you knew that.
OK, let's say your ACTUAL filename was "image analysis raw measurements.xlsx". So then you'd do
fullFileName = 'C:\Users\UserX\Desktop\image analysis raw measurements.xlsx'
[~, ~, imp] = xlsread(fullFileName);
Image Analyst
on 24 Sep 2017
Then what kind of file is your data IN? I was under the impressions it was in an Excel workbook file with a .xlsx extension. If it has nothing to do with Excel at all, then there are a variety of other functions you can use. You might be able to use several and it just depends on what your choice is. For example, maybe one or more of these will work: importdata, readtable, dlmread, csvread, fileread, fgetl, fread, textscan, fscanf, etc.
Walter Roberson
on 24 Sep 2017
You do not import code: you execute code. You use importdata to generate importfile.m and then you call
result = importfile('AppropriateDataFileNameGoesHere.txt');
mb12345
on 25 Sep 2017
Image Analyst
on 25 Sep 2017
So, what about xlsread(), like I showed, is not working? Attach an actual workbook if you want me to try to read it in for you.
Walter Roberson
on 26 Sep 2017
If you told it to generate code into filetoopen.m then to read your file whose name is stored in mystring then:
result = filetoopen(mystring);
Image Analyst
on 26 Sep 2017
Try
[~, strings, imp] = xlsread(fullFileName);
columnA = strings(:, 1); % Extract column 1 (A)
% Print words
for row = 1 : length(columnA)
thisString = columnA{k}
words = strsplit(thisString, ' ')
for w = 1 : length(words)
fprintf('In row #%d, word #%d = %s\n', row, w, words{w});
end
end
mb12345
on 27 Sep 2017
Walter Roberson
on 27 Sep 2017
Is C:\Users\UserX\Desktop on your MATLAB path?
And did you generate into filetoopen with no extension or into filetoopen.m ?
mb12345
on 27 Sep 2017
mb12345
on 28 Sep 2017
Ahsan Mehmood
on 16 Jun 2019
use length instead of len
Categories
Find more on Data Import from MATLAB 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!