Jorge Juarez
Jorge Juarez

Reputation: 33

Reading a Matlab file of numbers and printing them out

I have a file named signal.txt like so:

5.2530
5.2531
5.2534
5.2539
5.2544
5.2550

and I am trying to open the file and save the contents into a variable so that I may later use them for future use. What I have so far is this:

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);

Thank you and any help is appreciated.

Upvotes: 1

Views: 37

Answers (2)

Jorge Juarez
Jorge Juarez

Reputation: 33

the only thing wrong was formatSpec needs to equal '%f' instead of what I had previously:

formatSpec = '%f';

Upvotes: 1

pho
pho

Reputation: 25489

You don't need to write your own code to read the file. readmatrix(), or in older versions dlmread() will do the job for you.

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

Upvotes: 2

Related Questions