24小时热门版块排行榜    

查看: 1631  |  回复: 11
当前主题已经存档。
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

monitor2885

至尊木虫 (知名作家)

队长

[交流] 【求助】Matlab怎么读入csv文件

function test
clear
clc
global u yy
u0=xlsread('data.csv','A2:A12');%有错误提示呀
t0=0:1:10;
y0=[0 2];
tt=[];yy=[];
for i=1:length(t0)-1
    t=[t0(i), t0(i+1)];
    u=u0(i);
    [t,y]=ode45(@ivpodefun,t,y0);
    y0=y(end,: );
    tt=[tt;t];
    yy=[yy;y];
end

figure
plot(tt,yy(:,1),'ro-',tt,yy(:,2),'b^-')

function dydt=ivpodefun(t,y)
global u
dydt=[y(2);u*(1-y(1)^2)*y(2)-y(1)];

我的csv文件就在上传的压缩包里面。谢谢了

[ Last edited by monitor2885 on 2009-10-16 at 13:33 ]
回复此楼
Retirement
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

change0618

铁杆木虫 (著名写手)

方丈大师

★ ★ ★ ★
wuguocheng(金币+4,VIP+0):谢谢参与 10-16 16:40
function outputStuff = mfcsvread(fileName)
%mfcsvread reads a CSV file containing both text & numeric data.  MATLAB's
%csvread function will work with all numeric, or all text data, but not
%both.  It's common to have a file with a single line of comma separated
%text headers followed by many rows of numeric data.  xlsread is limited in
%the number of rows & colums (actually, Excel is the limitation) it can
%read.
%
% The CSV file should look like:
% comma, separated, text, ...
% 1,2,3,4,5,...
% 6,7,8,9,10,...
% etc...
%
% The output is a structure with the column headers as fields in a
% structure each with a vector of data.

% Open the file
fid=fopen(fileName);

% Start reading the data
tline = fgetl(fid); % Read in the first line only (text headers)
tline = tline(tline~=' '); %Get rid of any spaces
commaLocs=findstr(',',tline); % find the commas
fieldNames=cell(1,(length(commaLocs)+1));
start=1;
for colIdx=1:length(commaLocs)
    fieldNames{colIdx}=tline(start:commaLocs(colIdx)-1);
    start=commaLocs(colIdx)+1;
end
fieldNames{colIdx+1}=tline(start:end);

%Read in the rest of the data (should be numeric)
fieldData=csvread(fileName,1,0);

%Convert the data into a single structure.
[cellW cellH]=size(fieldNames);
numFields=max([cellW cellH]);
%Needs to be a 1xN or Nx1 cell-array.
if ~iscell(fieldNames)
    disp('Needs to be a cell-array');
    return;
elseif cellW ~=numFields && cellH ~=numFields
    disp('Needs to be 1xN or Nx1');
    return;
end

dataSize=size(fieldData);
arrayDim=find(dataSize==numFields);
if isempty(arrayDim)
    disp('Dimensions are wrong');
end

outputStuff=[];
if arrayDim==2
    for idx=1:numFields
        outputStuff.(fieldNames{idx})=fieldData(:,idx);
    end
else
    for idx=1:numFields
        outputStuff.(fieldNames{idx})=fieldData(idx,: );
    end
end

[ Last edited by change0618 on 2009-10-16 at 14:24 ]
10楼2009-10-16 14:16:15
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 12 个回答

monitor2885

至尊木虫 (知名作家)

队长

或者matlab能不能把csv文件转换成xls,然后再xlsread就行了
Retirement
2楼2009-10-15 22:39:08
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

change0618

铁杆木虫 (著名写手)

方丈大师

★ ★ ★ ★
monitor2885(金币+3,VIP+0): 10-15 23:55
sunxiao(金币+1,VIP+0):谢谢参与 10-16 02:53
你用load命令试一下
3楼2009-10-15 22:45:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

monitor2885

至尊木虫 (知名作家)

队长

u0=csvread('data.csv')不好用,因为我的文件第一行是文本,好像csvread要求所读取的csv文件必须都是数据。什么语句能删除csv文件里面的第一行,然后读取单元格A3到A10的数据?(假设csv里面有20*20的数据)

[ Last edited by monitor2885 on 2009-10-16 at 00:32 ]
Retirement
4楼2009-10-15 23:55:01
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见