xlsread not reading text only excel file

15 次查看(过去 30 天)
I am trying to get xlsread to read a excel file that has mostly text. This is my excel file.
test.xlsx
Patrice Oneal 22-Jun-2012 Bill Burr 9-Dec-10
For some reason MATLAB turns the variable with the filename to []. I can't figure out why. I don't think that the code is reading the test.xlsx file.I would like for MATLAB to put the file into an array, however MATLAB just stores the variable that is supposed to contain the array with a "[]". I don't understand why? Can someone explain what I am doing wrong, and how to fix it?
Here is my code:
% Clear all variables screen clear all
% Clear screen clc
% Ask user if they have an excel file they would like to read from prompt = ('Do you have an excel file to add to list? y/n \n'); user_ans_file = input(prompt,'s');
% If/else statement if the user says yes to having an Excel file
if user_ans_file == 'y'
% Ask user if the Excel file is in the write format
prompt = ('Is the excel file in the correct format [First Name|Last Name|dd-mmm-yyyy]? y/n \n');
user_ans_format = input(prompt,'s');
% If/else statement if the user says yes to having an Excel file in the write format
if user_ans_format == 'y'
% Ask user if the Excel file is in the write format
prompt = ('what is the name of the file with the .xlsx extention? \n');
file_name = input(prompt,'s');
% file_name = 'file_name'.xlsx;
% Read the Excel file that the user stated
num = xlsread(file_name);
end
else
end
--Ender--

采纳的回答

Iain
Iain 2014-8-8
You're only reading out the numbers, you want to read the file with:
[numbers, TEXT, everything] = xlsread(file_name);
TEXT{2,1} is cell A2.
  3 个评论
Ntwanano Baloyi
Ntwanano Baloyi 2022-5-21
This was very helpful. How do I read the data from other sheets because I used this and it read data from sheet 1 only
Walter Roberson
Walter Roberson 2022-5-21
Is there a reason you have not switched to readtable()?

请先登录,再进行评论。

更多回答(2 个)

Andy L
Andy L 2014-8-7
编辑:Andy L 2014-8-8
First of all your filename. You use the following lines of code to set up your file name:
file_name = input(prompt,'s');
Are you adding the .xlsx extension to the file name? This may be having an effect. You can add the extension by the following code:
file_name = [file_name,'.xlsx'];
As for the empty data array have you checked the matlab help for xlsread? num returns the numeric only data from the file. If you use the following format of the function
[num,txt,raw] = xlsread()
This should return the text data in a cell array txt. There is more information on the function in that link.
  1 个评论
madhan ravi
madhan ravi 2018-3-19
编辑:madhan ravi 2018-3-19
[num,txt,raw] = xlsread()-> only numbers are displayed even though the file contains text

请先登录,再进行评论。


Ender
Ender 2014-8-8
Thanks!
I originally told the user to delete the .xlsx extension, however you code works because I don't have to make that note to the user
--Ender--

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by