Can any one help me in adding new line space to the result. Thanks in advance

fid=fopen('largeadd.m','r');
result=' ';
while(feof(fid)~=1)
temp=char(fread(fid));
for i=1:max(size(temp))
if(~isspace(temp(i,1)))
result=strcat(result,char(temp(i,1)));
disp(isspace(temp(i,1)));
else
result=strcat(result,'\n');
end
end
end
end
disp(result);

Réponses (2)

result = strcat(result,sprintf('\n'));

2 commentaires

Thank you sir.
but my requirement is not obtained on using this statement.
The output obtained is:
%Additionoflongnumbersclc;num1=input('Enteravalue','s');num2=input('Enterbvalue','s');asize=max(size(num1));disp(size(num1));fori=1:asizea(1,i)=str2num(num1(1,i));disp(a(1,i));b(1,i)=str2num(num2(1,i));disp(b(1,i));end%disp(a);fori=1:asizec(1,i)=a(1,i)+b(1,i);disp(c(1,i));endfori=2:asizeif(c(1,i)>=10&&i>=2)c(1,i)=c(1,i)-10;c(1,i-1)=c(1,i-1)+1;endendresult='';if(c(1,1)>=10)c(1,1)=c(1,1)-10;result=strcat(result,num2str(1));endfori=1:asizeresult=strcat(result,num2str(c(1,i)));enddisp(result);
But the Expected is:
%Addition of long numbers
clc;
num1=input('Enter a value','s');
num2=input('Enter b value','s');
asize=max(size(num1));
disp(size(num1));
for i=1:asize
a(1,i)=str2num(num1(1,i));
disp(a(1,i));
b(1,i)=str2num(num2(1,i));
disp(b(1,i));
end
%disp(a);
for i=1:asize
c(1,i)=a(1,i)+b(1,i);
disp(c(1,i));
end
for i=2:asize
if(c(1,i)>=10&&i>=2)
c(1,i)=c(1,i)-10;
c(1,i-1)=c(1,i-1)+1;
end
end
result='';
if(c(1,1)>=10)
c(1,1)=c(1,1)-10;
result=strcat(result,num2str(1));
end
for i=1:asize
result=strcat(result,num2str(c(1,i)));
end
disp(result);
Any other results or answers welcome thanks in advance
fread(fid) reads the entire file.
max(size(temp)) can be replaced by length(temp)
I have to wonder why you are doing all of this. Why not just read the file and then use
temp(isspace(temp)) = sprintf('\n');

Connectez-vous pour commenter.

Why do all that? Just use fgetl() or fgets().
thisLine = fgets(fid);
lineWithExtraLineFeed = sprintf('%s\n', thisLine);

2 commentaires

By the way, you forgot to call fclose(fid).
The user's code changes every whitespace character to \n

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by