All I want to do (initially) is read an Excel or csv file

4 ビュー (過去 30 日間)
Frank
Frank 2011 年 11 月 7 日
Hi- I'm at the pre-novice level. Your patience is requested.
I have a csv file that has the top row containing column identifiers ( about 20 columns) and every row after that containing data.
All I want to do is read the data into Matlab. I've tried all sorts of commands but all I get is error messages. I don't want to use the import wizard--I need to learn how to write the commands.
How about a shove in the right direction?

採用された回答

Eric
Eric 2011 年 11 月 7 日
Something like the following should work:
%Prompt user for filename
[fname, pname] = uigetfile('*.csv');
%Create fully-formed filename as a string
filename = fullfile(pname, fname);
%Check that file exists
assert(exist(filename,'file')==2, '%s does not exist.', filename);
%Read in the data, skipping the first row
data = csvread(filename,1,0);
Good luck,
Eric
  2 件のコメント
Eric
Eric 2011 年 11 月 7 日
Useful things for novices in this code are:
1. Use of uigetfile() to prompt user for a filename
2. Use of the fullfile() function to create path-resolved filenames
3. Use of assert() to perform error checking
Note that if your version of Matlab is a bit dated, assert() may not work. It's been in Matlab for a few years now, but I forget when it first made its appearance.
A nice complement to fullfile() is fileparts().
-Eric
Frank
Frank 2011 年 11 月 7 日
Thank you for your help. I appreciate you coaching me with this problem.

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 11 月 7 日
try d=importdata('test.csv') and check the value of d in MATLAB workspace.
  5 件のコメント
Frank
Frank 2011 年 11 月 8 日
This is the better answer. The other answer uses commands like csvread that are going to be eliminated.
Eric
Eric 2011 年 11 月 10 日
I believe csvread has received a reprieve. In the Matlab R2010b Release Notes:
csvread and csvwrite Will Not Be Removed
The R2010a Release Notes originally stated that csvread and csvwrite would be removed in a future release. As of R2010b, there are no plans to remove these functions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by