How do I loop inside a text string?

조회 수: 16 (최근 30일)
Eugene Cho
Eugene Cho 2018년 4월 20일
편집: Walter Roberson 2018년 4월 20일
Hello All,
I am trying to create a text string to write lines of a solution based on the number of variables in that solution. The text string will be used to output the solution to a plot. Currently I am manually calling each solution like this:
figure(1)
hold on
plot(x,Tss(:,:),'LineWidth',2,'MarkerSize',5);
xlim([0 L])
x_label = xlabel('Mount Thickness,[m]');
y_label = ylabel('Temp [C]');
t_title = title('Mount SS Internal Temperture Profile');
str = {sprintf('Computational Domain:'),...
sprintf(' Mount Thickness: %.2f [in] %.2f [m]',L*25.4, L),...
sprintf(' Mount Width: %.2f [in] %.2f [m]',W*25.4, W),...
sprintf(' Mount Height: %.2f [in] %.2f [m]',H*25.4, H),...
sprintf('Boundary Conditions:'),...
sprintf(' B.C.1 at x=0 -k(dt/dx): %.1f [W/m2]',Q),...
sprintf(' B.C.2 at x=L -k(dt/dx): h[T(x=L)-Tinf, h = %.1f [W/m2K] Tinf = %.1f [C]',h, Tinf),...
sprintf('Steady State Results:'),...
sprintf(' %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] ',mat{1},Tss(1,1),Tss(1,end),deltaTss(1)),...
sprintf(' %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] ',mat{2},Tss(2,1),Tss(1,end),deltaTss(2)),...
sprintf(' %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] ',mat{3},Tss(3,1),Tss(1,end),deltaTss(3)),...
sprintf(' %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] ',mat{4},Tss(4,1),Tss(1,end),deltaTss(4)),...
sprintf(' %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] ',mat{5},Tss(5,1),Tss(1,end),deltaTss(5))};
text(.01,.99,str,'units','normalized','HorizontalAlignment','left',...
'VerticalAlignment','top','FontWeight','bold');
The last 5 lines of the string is what I would like to loop. The output of the above string looks like this below. This is what I would like but with a loop.
Steady State Results:
Al 6061-T651: T(x=0) = 37.29 [C] T(x=L) = 37.26 [C] DeltaT = 0.03 [C]
MMC 640XA AlSiC: T(x=0) = 37.91 [C] T(x=L) = 37.26 [C] DeltaT = 0.03 [C]
CRES 17-4 H900: T(x=0) = 38.81 [C] T(x=L) = 37.26 [C] DeltaT = 0.26 [C]
Copper OFHC: T(x=0) = 31.71 [C] T(x=L) = 37.26 [C] DeltaT = 0.01 [C]
Titanium 6A1-4V: T(x=0) = 57.61 [C] T(x=L) = 37.26 [C] DeltaT = 1.37 [C]
I have tried doing somthing like this:
sprintf(' %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] \n',mat{:},Tss(:,1),Tss(:,end),deltaTss(:))};
but the results are not what I want. The results are attached. I see that it might be trying to write out all the values at once for each variable rather than each cycling incrementally like what I want.
I've also tried writing the above text into a matrix cell like this:
A1 = " %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C]";
A2 = {mat{:},Tss(:,1),Tss(:,end),deltaTss(:)};
but I cant seem to get this to work correctly.
Thank you for the help!

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 20일

When you ask to format arrays, MATLAB proceeds along the columns, using all of the first input before moving on to the second input.

If your entries were purely numeric, then the pattern to use would be to change

sprintf(format, var1(:), var2(:), var3(:))

into

sprintf(format, [var1(:), var2(:), var3(:)].' )    %transpose is important

However you have a mix of numeric and character, so you cannot do simply that, because the character entries cannot be included in the same single array as the numeric entries/

So what you need to do instead is create a cell array with one entry for each value being output, along the lines of

%assuming mat is a cell array of character vectors and the other two are numeric
datacell = [mat(:), num2cell([Tss(:,[1 end]), deltaTss(:)])] .';  Transpose is important

then

sprintf(format, datacell{:})
  댓글 수: 1
Eugene Cho
Eugene Cho 2018년 4월 20일
편집: Walter Roberson 2018년 4월 20일

Excellent! That worked. Thank you for your help Walter.

textcell2 = [mat(:), num2cell([T1(:,1),T1(:,end),deltaT1(:)])].';
str2 = {sprintf('Computational Domain:'),...
    sprintf('   Mount Thickness: %.2f [in] %.2f [m]',L*25.4, L),...
    sprintf('   Mount Width: %.2f [in] %.2f [m]',W*25.4, W),...
    sprintf('   Mount Height: %.2f [in] %.2f [m]',H*25.4, H),...
    sprintf('Boundary Conditions:'),...
    sprintf('   B.C.1 at x = 0 -k(dt/dx): %.1f [W/m2]',Q),...
    sprintf('   B.C.2 at x = L -k(dt/dx): h[T(x=L)-Tinf, h = %.1f [W/m2K] Tinf = %.1f [C]',h, Tinf),...
    sprintf('   I.C. at T(x,t=0) = Ti = %.1f [C]', Ti),...
    sprintf('Transient Solution Results:'),...
    sprintf('   %s: T(x=0) = %.2f [C] T(x=L) = %.2f [C] DeltaT = %.2f [C] \n',textcell2{:})};

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by