读取数字的 Matlab 文件并打印出来

Reading a Matlab file of numbers and printing them out

我有一个名为 signal.txt 的文件,如下所示:

5.2530
5.2531
5.2534
5.2539
5.2544
5.2550

我正在尝试打开文件并将内容保存到一个变量中,以便以后使用 它们以备将来使用。我到目前为止是这样的:

clc
clear
close all

% This project will require you to implement the DFT computation for a 
% given signal in a MATLAB script and compare your output to the output of 
% the MATLAB fft() function.

formatSpec = '%1.4f\n';
fp = fopen('signal.txt','r');
numberArray = fscanf(fp,formatSpec);
fprintf('The number are: %f',numberArray)
fclose(fp);

谢谢您,我们将不胜感激。

您不需要编写自己的代码来读取文件。 readmatrix(), or in older versions dlmread() 会为您完成这项工作。

numberArray = readmatrix('signal.txt');
% or,
numberArray = dlmread('signal.txt');

唯一的错误是 formatSpec 需要等于 '%f' 而不是我之前的值:

formatSpec = '%f';