Writing string into array

3 Ansichten (letzte 30 Tage)
Anke Kügler
Anke Kügler am 20 Aug. 2016
Kommentiert: Image Analyst am 25 Aug. 2016
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 Kommentare
per isakson
per isakson am 20 Aug. 2016
With which releases (of Matlab) did it work and with which did it not?
per isakson
per isakson am 20 Aug. 2016

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 20 Aug. 2016
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 Kommentare
Anke Kügler
Anke Kügler am 20 Aug. 2016
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 am 25 Aug. 2016
What kind of array do you want, a cell array, or a character array? And tell me what "timestamp" is.

Melden Sie sich an, um zu kommentieren.


Anke Kügler
Anke Kügler am 20 Aug. 2016
Bearbeitet: Anke Kügler am 20 Aug. 2016
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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by