cell array

I have file :
2 3 5 7
10 12 0 1 1
3 41 23 1000 1 11 4 11
1 10 2
I want place this values in cell array
how do that?
thanks

댓글 수: 4

Fangjun Jiang
Fangjun Jiang 2011년 11월 27일
What do you mean by "file"?
Image Analyst
Image Analyst 2011년 11월 27일
Why on earth do you WANT the complication of a cell array when a simple numerical array would be much simpler and easier???
Sven
Sven 2011년 11월 27일
Huda, just a general piece of advice. It seems at the moment you are in the learning stages of MATLAB. That's fine, there's nothing wrong with that.
On one previous question you asked "how do I save a sparse matrix to a text file", and it turned out that you didn't need to use "sparse" *or* use a text file.
In this case you are asking how to store a sequence of numbers into a cell array. From your initial question (and as Image Analyst has asked), it seems that you don't *need* to use a cell array, as that would only complicate things.
Here's my suggestion: try to describe very clearly *what you have*, *what you are trying to do*, and *why*. I have a feeling (from your previous questions) that if you can describe these things clearly, you might get some very good answers that will show you the best way how to do it and help you learn much faster.
In this question we don't quite know *what you have* (see Fangjun's question) or *why* you're trying to store to a cell when a matrix would be more appropriate.
huda nawaf
huda nawaf 2011년 11월 27일
I want use cell array in order to avoid insert zeroes in array, because each row in my file with different length.
I do all that to avoid out of memoery
the above data is just example ,my data is very huge.
thnaks

답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 27일

0 개 추천

YourCell = {};
fid = fopen('YourFile.txt','rt');
numrows = 0;
while true
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
numrows = numrows + 1;
YourCell{numrows} = sscanf(thisline, '%g');
end
fclose(fid);

댓글 수: 4

Image Analyst
Image Analyst 2011년 11월 27일
Since you posted this, huda has now added the constraint that zeros in the data should not get added to the cell array. But this shouldn't be too hard for him to add since he's been programming in MATLAB for many months now.
Jan
Jan 2011년 11월 27일
Do you mean "YourCell{numrows} = sscanf('%g', thisline);" with curly braces?
Image Analyst
Image Analyst 2011년 11월 27일
Not exactly, but just because the sscanf args are reversed. Either of these two would work though:
% Method 1
YourCell(numrows) = {sscanf(thisline, '%g')};
% Method 2
YourCell{numrows} = sscanf(thisline, '%g');
As far as I can tell, they're the same. But it's this just kind of brace/parentheses confusion that is confusing to beginners.
Walter Roberson
Walter Roberson 2011년 11월 28일
Thanks, code adjusted to remove errors.

이 질문은 마감되었습니다.

태그

질문:

2011년 11월 27일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by