Info

This question is closed. Reopen it to edit or answer.

Reading many txt files to import some vectors

1 view (last 30 days)
mahmoud el majzoub
mahmoud el majzoub on 7 Feb 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Dear Users I have question about reading txt files that i attached them if someone could help please. write a program in Matlab that reads output files like the ones above for a number of different ions and makes plots of the transition rates A, the gf values or the S value, as decided by the user, for transitions between selected upper and lower state symmetries along the iso-electronic sequence. The user should be able to specify the names of the files to be read and also give the nuclear charge Z corresponding to each file. The user should be able to specify the file (f1 or f2), the symmetry and the number within the symmetry for the upper state and the file, the symmetry and the number within the symmetry for the lower state. Your program should read data in all files and identify if the given transition is present in all data files. If the specified transition is present in all data files your program should plot the transition as a function of Z. If the specified transition is not present in all data files your program should notify the user asking him to supply new symmetries and levels.
I did the following script as filename_txt = 'transition'; lines = {}; line_num = 0; index=0; all_arrays = {}; array_size = 0; first_file=22;last_file=24;
for k=22:24 filename = [filename_txt,num2str(k),'.txt']; d = fopen(filename, 'r'); if (d < 0) fprintf('file %s does not exit\n', filename); continue; end %reading the lines from first to dash2 and do nothing for them tline = fgets(d);
while ischar(tline)
tline=fgets(d);
if (strncmpi(tline, ' f', 2) || strncmpi(tline, ' ', 6))
split = regexp(tline, '\s+', 'split');
if (length(split) > 5)
line_num = line_num + 1;
lines{line_num} = split;
end
end
end
disp(lines);
fclose(d);
end
File=(char());
Upperlev = zeros();
UpperJ = zeros();
Upperparity = (char());
File2=(char());
Lowerlev = zeros();
LowerJ = zeros();
Lowerparity = (char());
A=zeros();
gf=zeros();
S=zeros();
A2=zeros();
gf2=zeros();
S2=zeros();
index_f2 = 2;
index_upperlev=3;
index_upperJ=4;
index_upperparity=5;
index_f1=6;
index_lowerlev=7;
index_lowerJ=8;
index_lowerparity=9;
index_A=12;
index_gf=13;
index_s=14;
index_A2=3; index_gf2=4; index_s2=5;
for i=1:line_num if (length(lines{i}) == 15) File=lines{1,i}{1,index_f2};
Upperlev= str2num(lines{1,i}{1,index_upperlev});
% Upperlev(File)=Upperlev;
UpperJ = str2num(lines{1,i}{1,index_upperJ});
%UpperJ(File)=UpperJ;
Upperparity = lines{1,i}{1,index_upperparity};
%Upperparity(File)=Upperparity;
File2=lines{1,i}{1,index_f1};
%File2(File)=File2;
Lowerlev= str2num(lines{1,i}{1,index_lowerlev});
%Lowerlev(File)=Lowerlev;
LowerJ = str2num(lines{1,i}{1,index_lowerJ});
%LowerJ(File)=LowerJ;
Lowerparity = lines{1,i}{1,index_lowerparity};
A= str2num(lines{1,i}{1,index_A});
%disp(A)
gf= str2num(lines{1,i}{1,index_gf});
S= str2num(lines{1,i}{1,index_s});
%Lowerparity(File)=Lowerparity;
%fprintf('%s\t%d\t%d\t%s\t%s\t%d\t%d\t%s\n' ,File,Upperlev,UpperJ,Upperparity,File2,Lowerlev,LowerJ,Lowerparity) ;
%{
disp(lines{i}(index_lowerparity));
%}
end
if (length(lines{i}) == 6)
A2= str2num(lines{1,i}{1,index_A2});
gf2= str2num(lines{1,i}{1,index_gf2});
S2= str2num(lines{1,i}{1,index_s2});
%fprintf('%d\t%d\t',A,gf,S)
end
fprintf('%s\t%d\t%d\t%s\t%s\t%d\t%d\t%s\t%d\t%d\t%d\n\t\t\t\t\t\t\t\t%d\t%d\t%d\n' ,File,Upperlev,UpperJ,Upperparity,File2,Lowerlev,LowerJ,Lowerparity,A,gf,S,A2,gf2,S2) ;
index = index + 1;
if (length(lines{i}) == 15)
all_arrays{index, index_f2} = File;
all_arrays{index, index_upperlev} = Upperlev;
all_arrays{index, index_upperJ} = UpperJ;
all_arrays{index, index_upperparity} =Upperparity;
all_arrays{index, index_f1} = File2;
all_arrays{index, index_lowerlev} = Lowerlev;
all_arrays{index, index_lowerJ} = LowerJ;
all_arrays{index, index_lowerparity} = Lowerparity;
all_arrays{index, 10} = A;
all_arrays{index, 11} = gf;
all_arrays{index, 12} = S;
end
if (length(lines{i}) == 6)
all_arrays{index, 10} = A2;
all_arrays{index, 11} = gf2;
all_arrays{index, 12} = S2;
end
% disp(all_arrays);
array_size = length(lines);
end user_f2 = input('Enter the file data for upper : '); user_lev1 = input('Enter the upper level : '); user_J1 = input('Enter the value of angular momentum: '); user_parity1 = input('Enter the parity: '); user_lev2 = input('Enter the lower level : '); user_J2= input('Enter the value of angular momentum 2 : '); user_parity2 = input('Enter the parity2 : '); user_f1 = input('Enter the file data for lower : ');
user_vector = {};% change to double vector_number = 0;
for i=1:array_size first_index = 1; % this is the first file
vector_index = 0;
f2=all_arrays{first_index, index_f2}(i);
j_1 = all_arrays{first_index, index_upperJ}(i);
parity_1 = all_arrays{first_index, index_upperparity}(i);
lev_1= all_arrays{first_index, index_upperlev}(i);
f1=all_arrays{first_index, index_f1}(i);
j_2 = all_arrays{first_index, index_lowerJ}(i);
parity_2 = all_arrays{first_index, index_lowerparity}(i);
lev_2 = all_arrays{first_index, index_lowerlev}(i);
if (user_f2==f2 && user_J1 == j_1 && user_parity1 == parity_1 && user_lev1==lev_1 && user_J2 == j_2 && user_f1==f1 && user_parity2 == parity_2 && user_lev2==lev_2 )
disp(i);
vector_number = vector_number + 1 ;
for file_index=first_index:index
vector_index = vector_index + 1 ;
user_vector{vector_number}(vector_index) = all_arrays{file_index,all_arrays(2:2:end,10:12)}(i);
end
%break;
end
end
My problem when i run it i got the following message ??? Operands to the and && operators must be convertible to logical scalar values.
Error in ==> try2 at 213 if (user_f2==f2 && user_J1 == j_1 && user_parity1 == parity_1 && user_lev1==lev_1 && user_J2 == j_2 && user_f1==f1 && user_parity2 == parity_2 && user_lev2==lev_2 ) Any one can help please as soon.

Answers (1)

Image Analyst
Image Analyst on 7 Feb 2015
Chances are some of those things are not single values, but arrays. Hover the mouse over them and find out which variables are not single scalar numbers.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!