如何使用Matlab在表格中显示字符串 [英] How to display string in table using matlab

查看:1065
本文介绍了如何使用Matlab在表格中显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序代码:

fileID = fopen('PROBSET_102.csv','w');
a= 3;
lamda = 1.54;

fprintf(fileID, ' h, k, l ,d, theta \n');
for h = -8:8
   for k = -8:8
       for l = -8:8
           d = sqrt((a^2)/(h^2 + k^2 + l^2));
           c = (lamda/(2*d));
           if c>1
               theta = ('out of range');
           else 
               theta = asind(c);

           end
            fprintf(fileID, ' %d, %d, %d, %d, %d\n',h,k,l,d,theta);
       end
   end
end


fclose(fileID);

我正在尝试在表格上打印以下值:h,k,l,d,theta.但是,当c值大于1时,theta列应显示超出范围",而不是实际输出的数字.请帮忙.

I am trying to print the following values on a table: h, k,l,d,theta. But when c value is greater than 1, the theta column should display 'out of range' instead of the actual number output. Please help.

推荐答案

最接近原始代码的解决方案:

正如另一个建议那样,您的代码错误,因为您正在将不同类型的数据发送到fprintf.您可以为fprintf指定不同的format,然后在代码的条件部分中以此特定格式调用它(其他2种解决方案向您展示如何实现).

Solution closest to your original code:

As the other suggested, your code error because you are sending data of different type to the fprintf. You can either specify a different format for fprintf and call it with this specific format in the conditional part of you code (the other 2 solutions show you how to do that).

如果要将fprintf调用保留在条件分支之外,只需确保参数theta始终是同一类型(在这种情况下为string)即可.

If you want to keep your fprintf call outside of the conditional branch, just make sure your parameter theta is always the same type (a string in this case).

由于最终结果将仍然是将数字转换为文件中的字符串,因此我们可以尽早对此变量进行操作,并确保fprintf始终接收相同的类型.

Since the final result will be to convert numbers to string in the file anyway, we can do it early for this variable and make sure fprintf always receive the same type.

if c>1
   theta = 'out of range' ;             %// this is a string
else 
   theta = sprintf( '%d' , asind(c) ) ; %// this is also a string
end
fprintf(fileID, ' %d, %d, %d, %d, %s\n',h,k,l,d,theta); %// last parameter printed is a string


或者:

如果您对应该为"超出范围"值显示的文本比较灵活(也就是说,如果您可以接受使用"NaN"代替),那么请按NaN代表 N ot A N 的数字,但在这里并不重要)是有效的数字双精度值,可以通过fprintf(系列)功能无缝处理.


Alternatively:

If you are flexible on the text which should be displayed for the "out of range" values (that is if you can accept to have "NaN" instead), then NaN (it stands for Not A Number, but it's not important here) is a valid numeric double precision value which can be handled seamlessly by fprintf (and family of) function.

这意味着您可以摆脱三重嵌套的for循环并向量化完整代码.在我的计算机上,它的执行速度大约快10倍(计算+文件写入),但是您的工作量可能会有所不同.如果您计划扩大网格大小,那么速度的增长将更加有趣,尽管在某种程度上,内存将成为您的限制因素.

It means that you could get rid of you triple nested for loop and vectorize your full code. On my machine it execute about 10x faster (calculations + file writing) but your mileage may vary. If you plan on enlarging the grid size, the gain in speed will be even more interesting, although there will be a size where the memory will be your limiting factor.

矢量化代码如下:

a= 3;
lamda = 1.54;

gridBase = -8:8 ; %// create a vector [-8 -7 -6 .... 6 7 8]

[L,K,H] = ndgrid(gridBase,gridBase,gridBase) ;  %// Generate a grid
D = sqrt( (a.^2) ./ (H.^2 + K.^2 + L.^2) );     %// calculate D in one block (over the full grid)
C = lamda ./ (2.*D);                            %// same for C
C(C>1) = NaN ;                                  %// make values of C>1 stand out (assign then to "Not a Number" NaN)
A = [H(:) K(:) L(:) D(:) C(:)] ;                %// collate all that in a big table (equivalent to what you'll have in your file)

%// now write the array into file
fileID = fopen('PROBSET_102_alternate.csv','w');
fprintf(fileID, ' h, k, l ,d, theta \n');       %// write header line
fprintf(fileID, ' %d, %d, %d, %d, %d\n' , A.'); %'// let Matlab manage the write loop in the background (much faster)
fclose( fileID);


如果您需要了解代码,我邀请您逐行运行以上代码(首先从较小的网格大小开始),然后在工作区资源管理器中查看变量.


If you need to understand the code, I invite you to run the above line by line (starting with a small grid size first) and look at the variables in the workspace explorer.

这篇关于如何使用Matlab在表格中显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆