How can I change the arrangement of values and plot them?
2 次查看(过去 30 天)
显示 更早的评论
Hello, I am working on a project using Matlab's GUI. However, there was a problem. I'd like to retrieve the value of the text file as shown in the first picture. However, the results are printed like the second picture. I want the same result as the third picture. What should I do? With str2double, the NaN value is output. And my final goal is to plot these values. help :(
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1015630/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1015635/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1015640/image.png)
used code
str = extractFileText('uncoded_BER.txt');
assignin('base', 'str', str);
6 个评论
Hiro Yoshino
2022-5-30
If you have multiple data
data=readtable("uncoded_BER.txt")
data.var1
would be userful as you can access multiple columns easily.
回答(1 个)
Yash
2023-11-15
Hello,
I understand that you are interested in importing the the data from a text file as a matrix (or array) and plotting it. Currently, you are using the "extractFileText" function, which reads the entire text from the file and stores it as a string. In your case, it is '-1.5093\n3.4562' where '\n' represents the new line character. This can't be converted to an array using the "str2double" function as it is not in a proper numeric form. Hence you are getting NaN.
There are several other functions you can utilize to import data as a matrix (or array). A few of them are listed below:
1. "importdata" - This can be used to load data from a file as a matrix. The documentation is available at the following link: https://www.mathworks.com/help/matlab/ref/importdata.html
A = importdata('uncoded_BER.txt')
2. "load" - This function is used to load variables from a file into the workspace. Refer to the following link for the documentation: https://www.mathworks.com/help/matlab/ref/load.html
A = load('uncoded_BER.txt')
3. "readmatrix" - This function is specifically designed to read a matrix from a file. You can refer the documentation here: https://www.mathworks.com/help/matlab/ref/readmatrix.html
A = readmatrix('uncoded_BER.txt')
4. "readtable" - This function creates a MATLAB table from a file. This would be beneficial if you have multiple data labellled columnwise. The documentation is available at the following link: https://www.mathworks.com/help/matlab/ref/readtable.html
TBL = readtable('uncoded_BER.txt')
A = TBL.Var1
You can use "textscan" as well, refer to the below MATLAB Answer for an example:
Once you have successfully imported the data as a matrix or array, you can use the "plot" function to visualize it. Refer to the documentation of the plot function at the following link: https://www.mathworks.com/help/matlab/ref/plot.html
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!