Matlab4dPlot(x,y,z,t)
我试图绘制一个包含x,y,z,t的数千个数据点的图表。数据位于.txt文件中,如下所示:Matlab4dPlot(x,y,z,t),matlab,4d,Matlab,4d,我试图绘制一个包含x,y,z,t的数千个数据点的图表。数据位于.txt文件中,如下所示: [x,y,z,时间] [50.9160,12.2937,-44.9963,0.0] [50.9160,12.2937,-44.9963,0.8] [50.9160,12.2937,-44.9963,1.8] [50.9160,12.2937,-44.9963,2.8] [50.9160,12.2937,-44.9963,3.8] [50.9160,12.2937,-44.9963,4.9] [50.916
- [x,y,z,时间]
- [50.9160,12.2937,-44.9963,0.0]
- [50.9160,12.2937,-44.9963,0.8]
- [50.9160,12.2937,-44.9963,1.8]
- [50.9160,12.2937,-44.9963,2.8]
- [50.9160,12.2937,-44.9963,3.8]
- [50.9160,12.2937,-44.9963,4.9]
- [50.9160,12.2937,-44.9963,8.8]
- [50.9160,12.2937,-44.9963,11.1]
- [50.9160,12.2937,-44.9963,11.7]
- [50.9160,12.2937,-44.9963,12.8]
- [50.8989,12.3248,-45.0376,13.7]
- [50.8989,12.3248,-45.0376,14.9]
- [50.8989,12.3248,-45.0376,15.7]
- [50.8989,12.3248,-45.0376,17.2]
- [50.8989,12.3248,-45.0376,17.7]
- etc(超过1000个数据点)
我在考虑打开文本文件,创建一个循环,捕捉x,y,z,t并使用scatter3进行绘图。如果有人能让我开始了解MATLAB代码应该是什么样子的,那就太好了。我真的不明白你想如何在4D中绘图,但我猜你希望在3D中绘图,然后随着时间的推移动态更改绘图 但无论如何,关于文件: 首先,文本阅读非常简单
文件\u text\u rd
,然后使用此变量打开文件:
file_text_rd=fopen('data.txt','r')代码>
第一个参数是.txt
文件的名称(请注意,您需要将目录设置为.txt
文件所在的文件夹)。第二个参数表示希望从文本文件中读取
var=fgetl(文件文本)代码>
将文件内容的第一行放入变量中
var=fscanf(文件文本%c)代码>
将把.txt
文件的全部内容放入变量等。其他读取函数有fread
和fgets
。因此,根据函数的不同,您可能需要使用一些循环函数来用内容填充var
fclose(file\u text\u rd),clear file\u text\u rd代码>
fscanf
函数
%Open file for reading
file_text_rd=fopen('data.txt','r');
%Read the content (%c means you are reading characters)
var=fscanf(file_text_rd,'%c');
%Converse the characters to double. Have in mind the ascii values of the
%chars, so you can get the actual number value of the numbers in the string
%by subtracting the 48 of the original value, since the zero in ascii is
%numbered as 48 (in decimal system).
conv_var=double(var)-48;
%Define the initial value of your variable (all zeros)
final_var=zeros(1,4);
%Row counter
count_r=1;
%Column counter
count_c=1;
%Divider
times=10;
%Dot flag
dot=0;
%Negative sign flag
negative_sign=0;
%This for loop is for testing every single character from the first to the
%last
for i=1:size(conv_var,2)-1
%This if condition is for:
%1. Checking if the character is a number between 0 and 9
%2. Checking if the character is a dot
%3. Checking if the character is a minus sign
%4. Checking if the character is a comma
%All other characters are not of interest.
if (conv_var(i)>=0 && conv_var(i) <=9) || conv_var(i) == -2 || conv_var(i) == -3 || conv_var(i) == -4
%If it's not a comma (that means we are still on the last number we
%were working on) we go in this section
if conv_var(i)~= -4
%If it's not a minus sign we go in this section
if conv_var(i) ~= -3
%If the dot flag hasn't been set to 1 yet (no dot in the
%string has yet been found) we don't enter this section
if dot==1
%If the flag HAS been set, then the number just found
%on the sequence is divided by 10 and then added to the
%old versison, since if we are reading the number
%'50.9160', the 9 has to be divided by 10 and then
%added to 50
final_var(count_r,count_c)=final_var(count_r,count_c)+conv_var(i)/times;
%The divider now rizes because the next found number
%would be 10 times smaller than the one found just now.
%For example, in '50.9160', 1 is 10 times less than 9
times=times*10;
else
%This condition is needed so we don't add the ascii
%number equivalent to the dot to the number we are
%working on.
if conv_var(i)~=-2
%We multiply the old version of the number we are
%working on, since if we are reading the number
%'50.9160', first we will read 5, then we will read 0,
%so we will need to multiply 5 by 10 and then add the 0
%and so on...
final_var(count_r,count_c)=final_var(count_r,count_c)*10+conv_var(i);
else
%If the character found IS the dot, then we just
%set the flag
dot=1;
end
end
else
%If the character found IS the negative_sign, then we set
%the flag for the negative_sign, so we can multiply the
%number we are working on atm with -1.
negative_sign=1;
end
else
%We get in this section if we found a comma character (or the
%ascii equvalent of the comma sign, more accurately)
if negative_sign==1
%This is the part where we multiply the number by -1 if
%we've found a minus sign before we found the comma
final_var(count_r,count_c)=-final_var(count_r,count_c);
end
%Here we add 1 to the column counter, since we are ready to
%work with the next number
count_c=count_c+1;
%We reset all the flags and the divider
dot=0;
times=10;
negative_sign=0;
end
end
%The number -38 in ascii is the equivalent of NL, or the end of the
%line sign (which we can't see), which actually means there was an "Enter" pressed at this point
if conv_var(i)==-38
%We set the column counter to one since, we will work now with the
%first number of the next four parameters
count_c=1;
%We increment the row counter so we can start saving the new values
%in the second row of our matrix
count_r=count_r+1;
%We set the next row initially to be all-zeros
final_var(count_r,:)=zeros(1,4);
%We reset the flags
dot=0;
times=10;
negative_sign=0;
end
end
%We close the file, since our work is done (you can put this line after the
%fscanf if you like)
fclose(file_text_rd), clear file_text_rd;
%打开文件进行读取
file_text_rd=fopen('data.txt','r');
%阅读内容(%c表示您正在阅读字符)
var=fscanf(文件、文本、%c');
%将字符转换为双字符。记住
%字符,因此您可以获得字符串中数字的实际数值
%通过减去原始值的48,因为ascii中的零是
%编号为48(十进制)。
conv_var=double(var)-48;
%定义变量的初始值(全部为零)
最终值=零(1,4);
%行计数器
计数r=1;
%列计数器
计数c=1;
%分隔器
次数=10次;
%点旗
点=0;
%负号旗
负号=0;
%这个for循环用于测试从第一个字符到第二个字符的每个字符
%最后
对于i=1:size(conv_变量,2)-1
%该条件适用于:
%1.检查字符是否为介于0和9之间的数字
%2.检查字符是否为点
%3.检查字符是否为减号
%4.检查字符是否为逗号
%其他所有角色都不感兴趣。
如果(conv_var(i)>=0&&conv_var(i)Matlab文档不能让你开始吗?请在提问之前尝试一些东西并发布一些代码:首先,Sas Dan说你应该阅读这些文档并尝试其中的一些示例。我能看到的最大问题是如何生成时间。对于这个问题,我认为时间的颜色编码最好,但很难说是肯定的在看到结果之前。此外,将时间作为颜色代码有点违反直觉。然而,这将有助于为线条指明方向(因为数据是采样的)嘿。看看4D绘图的不同可能性:或者谢谢你的帮助!我还有一个更进一步的问题。在我的数据中,我在括号前有一个负号。例如:-[50.9160,12.2937,-44.9963,0.0]-[50.9160,12.2937,-44.9963,0.8]。你能修改你的代码以便不考虑开头的负号吗?