Reading selected characters from a cell
9 ビュー (過去 30 日間)
古いコメントを表示
'a' and 'b' are my files (attached), I have used textscan to read both files in string. After merging all files into one cell i.e. 'Y', now I want to read selected characters within a cell and need to shift on the 1st position. For example, in file 'a' 714 is at the beginning of the line, I need to replace 714 at the place of 504 in the same line. I have to do replacement in all lines and the same for the text file 'b'. Anybody could help me how to read selected characters in all files in loop? My code is given below:
[filenames, pathname] = uigetfile('E:\matlab\*.txt','MultiSelect','on'); filenames = cellstr(filenames);
for k = 1:length(filenames);
textFilename = ['filenames' num2str(k) '.txt'];
textFilename=fullfile(pathname, filenames{k});
OpnFile = fopen(textFilename, 'r');
Data{k} = textscan(OpnFile,'%s','Delimiter','\n');
Data_new=[Data{:}];
A={};
A{end+1}=Data_new;
fclose(OpnFile);
end
X=[A{:}];
Y=[X{:}];
Thanking in advance.
2 件のコメント
Jan
2017 年 8 月 1 日
The question is not clear:
After merging all files into one cell i.e. 'Y', now I want to read
selected characters within a cell and need to shift on the 1st
position
"All files" means "both files? What does "cell" mean here? Which characters are "selected"? Does "within a cell" means "within a row"? What is "shift" here and what is the "1st position"?
replace 714 at the place of 504
Why "504"?
The file import might be simpler with:
A = cell(1, numel(filenames));
for k = 1:length(filenames)
Filename = fullfile(pathname, sprintf('filenames%d.txt', k));
A{k} = strsplit(fileread(Filename), '\n');
end
回答 (1 件)
Robert U
2017 年 8 月 2 日
Hi Marine Sachel,
Just to show you one possible way to exchange parts of strings, here a solution for your 1st row and 1st column. Using strfind() you can determine the searchvalues that are used to separate columns or the number of digits is known and you can directly address the digits to copy into the columns.
searchitems = {'714','504'};
replaceitems = {'504','714'};
out{1,1} = Y{1,1};
for ik = 1:numel(searchitems)
temp = strfind(Y{1,1},searchitems{ik});
if ~isempty(temp)
for jk = 1:length(temp)
out{1,1}(temp(jk):temp(jk)-1+length(searchitems{ik})) = replaceitems{ik};
end
end
end
Kind regards,
Robert Uhlig
3 件のコメント
Robert U
2017 年 8 月 2 日
編集済み: Robert U
2017 年 8 月 2 日
I think the answer to your question is already included in the code snippet.
Address a particular number of characters in a cell:
out{1,1}(StartChar:EndChar)
So if you would like to exchange always characters 1:3 with 9:11 and vice versa the code could look like this (assuming that the values to exchange are at same position in the string at all times):
out{1,1}(1:3) = Y{1,1}(9:11);
out{1,1}(9:11) = Y{1,1}(1:3);
Finally, you would have to implement a loop for addressing all of your cells containing the desired data.
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!