xlsread not reading text only excel file

조회 수: 6 (최근 30일)
Ender
Ender 2014년 8월 7일
댓글: Walter Roberson 2022년 5월 21일
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--

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by