필터 지우기
필터 지우기

Writing string into array

조회 수: 2 (최근 30일)
Anke Kügler
Anke Kügler 2016년 8월 20일
댓글: Image Analyst 2016년 8월 25일
Hi,
I'm trying to write strings into an array, but for some reason it's not working as expected (and has been working perfectly in other code I have)
So I have in a for loop:
newtimearray(n,:)=[datestr(timestamp,'yy') datestr(timestamp,'mm')...
datestr(timestamp,'dd') datestr(timestamp,'HH') datestr(timestamp,'MM')...
datestr(timestamp,'ss') strcat(fileID,'-',num2str(chunkCnt),'.bin') 'normal'];
However, instead of saving every entry into a single cell, it merges everything into one single string.
newtimearray =
16051512100100000000-6.binnormal
16051512400100000001-6.binnormal
16051513100100000002-6.binnormal
As I said, similar code is working fine and I don't see the error.
Thanks :)
  댓글 수: 2
per isakson
per isakson 2016년 8월 20일
With which releases (of Matlab) did it work and with which did it not?
per isakson
per isakson 2016년 8월 20일

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2016년 8월 20일
You're writing them into a regular character array because you used square brackets, not into a cell array, which requires braces. To see the differences and to learn when to use braces, brackets, and parentheses, see the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
  댓글 수: 2
Anke Kügler
Anke Kügler 2016년 8월 20일
I did try curly braces and got the error 'Conversion to char from cell is not possible.'
Also, as I mentioned, the exact same syntax is working with another code.
Image Analyst
Image Analyst 2016년 8월 25일
What kind of array do you want, a cell array, or a character array? And tell me what "timestamp" is.

댓글을 달려면 로그인하십시오.


Anke Kügler
Anke Kügler 2016년 8월 20일
편집: Anke Kügler 2016년 8월 20일
This is the code that works, producing a cell-array
for k = 2 : numel(subfolder)
wavefiles=dir(char(subfolder(k)));
fullname=fullfile(char(subfolder(k)),wavefiles(5).name);
fid = fopen(fullname);
fseek(fid,364,'bof');
datestring=char(fread(fid,[1, 24],'char'));
subfolderstr=char(subfolder(k));
scene= cellstr(subfolderstr(end-12:end-5));
recording=cellstr(subfolderstr(end-3:end));
date=datestr(datenum(sprintf(datestring),'yyyy-mm-ddHH:MM:SS'),'mm/dd/yyyy'); %get date from timestamp
time=datestr(datenum(sprintf(datestring),'yyyy-mm-ddHH:MM:SS'),'HH:MM:SS'); %get time from timestamp
timestamps(k-1,:)=[scene recording date time];
end
And this is the complete non-working code
%get the timestamp
logid=fopen([inlogpath, inlogfile], 'r');
filetimearr=textscan(logid, '%d %d %d %d %d %d %s %s','headerlines', 1);
yr=filetimearr{1}(n);
mo=filetimearr{2}(n);
dy=filetimearr{3}(n);
hr=filetimearr{4}(n);
mins=filetimearr{5}(n);
sec=filetimearr{6}(n);
type=filetimearr{8}(n);
timestamp=datenum(strcat(num2str(yr),sprintf('%02d',mo),sprintf('%02d',dy),sprintf('%02d',hr),sprintf('%02d',mins),sprintf('%02d',sec)),'yyyymmddHHMMSS');
fid=fopen(fullfilename);
[sig,TotalSamples] = fread (fid,inf,'int16');
chunkCnt=1;
Fs=125000;
numSamplesPerChunk = chunkDuration*Fs;
for l=1:numSamplesPerChunk:TotalSamples
fseek(fid,l,'bof');
y=fread(fid, numSamplesPerChunk);
outFileName = strcat(fileID,'-',num2str(chunkCnt),'.bin');
fulloutname=fullfile(savepath,outFileName);
fidsave = fopen(fulloutname,'w');
fwrite(fidsave,y,'int16');
fclose(fidsave);
newtimearray(n,:)=[datestr(timestamp,'yy') datestr(timestamp,'mm') datestr(timestamp,'dd') datestr(timestamp,'HH') datestr(timestamp,'MM') datestr(timestamp,'ss') strcat(fileID,'-',num2str(chunkCnt),'.bin') 'normal'];
timestamp=addtodate(timestamp,chunkDuration,'second');
chunkCnt = chunkCnt + 1;
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by