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
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
Marine Sachel
Marine Sachel 2017-8-2
Alright let me clarify it more, all files means text files 'a' and 'b'. When you will run my code on your MATLAB, you will be able to decipher my question quite well. Anyhow, screenshot is also attached herewith. Refer to the screenshot, you can see that 'Y' is a cell containing contents of two text files 'a' and 'b' as shown in Array Editor. Now refer to 1st row and 1st column of Y i.e. Y{1,1}, which is '714004445040...' here I want that '504' should be replaced with '714' so the output will be '504004447140...' Same replacements I have to do for all rows and columns of Y, and then I want to split them in separate columns for instance, 504 in one column, 004 in another, 447 in the 3rd column, 140 in 4th and so on with 3 digits.
Can you help me with this? Thanks.

请先登录,再进行评论。

回答(1 个)

Robert U
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
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.
Marine Sachel
Marine Sachel 2017-8-2
Oh Bravo! It worked! My arduous job got easy! Thanks alot!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by