Fourier Transform for xlsx files from excel

I have files from Excel in xlsx format that I want to find the fourier transform of. I have the code to generate a Fourier Transform but I'm not sure how to incorporate the files into the code itself.
clc;
clear all;+5
close all;
%This script generates a 20 Hz size wave of unit amplitude, of duration 1
%second, and time resolution of 1E-2 seconds; then calculates the
%single-sided Fourier transform and plots both quantities
dt=1E-2; %s
T=1; %s
f_sig=20; %Hz
t=[0:dt:T-dt]; %time vector
y=sin(2*pi*f_sig*t); %amplitude vector
subplot(121)
plot(t,y,'-b','LineWidth',2); %plot the time series
xlabel('time (s)');
ylabel('Amplitude (V)');
title('Time domain signal')
set(gca,'FontSize',20,'LineWidth',2)
F = fft(y); %calculate the Fast Fourier Transform
Fs=1/dt; %define the sampling frequency
L=length(t); %define the length of your time and amplitude vectors
P2 = abs(F/L); %take the normalized amplitude of your Fourier transform
P1 = P2(1:L/2+1); %adjust the length and scale to get single sided spectrum
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L; %create your frequency vector
subplot(122)
plot(f,P1,'-r','LineWidth',2); %plot the Fourier transform
xlabel('frequency (Hz)');
ylabel('Amplitude (V)');
set(gca,'FontSize',20,'LineWidth',2)
title('Fourier Transform')
xlim([0 50])

6 Comments

I have some more additional code that imports the xlsx files but it's giving me errors for the num, input commands.
%this loop extracts data from excel files and fills them up in inputand
%output matrices - each column corresponds to 1 signal acquisition
%for input
for i=1:num
filename=sprintf('NewFile1_in.xlsx',i); %opens file for reading
filename2=sprintf('NewFile1_out.xlsx',i);
sheet=1; %sheet number in excel file
xlRange1='B2:B15039'; %row range of Channel 1 & 2
Input(:,i)=Input(:,i)+xlsread(filename,sheet,xlRange1);
Output(:,i)=Output(:,i)+xlsread(filename2,sheet,xlRange1);
end
filename3=sprintf('NewFile1_in.xlsx');
xlRange3='A2:A15039';
time_vec=xlsread(filename3,sheet,xlRange3);
Guillaume
Guillaume on 17 May 2019
Edited: Guillaume on 17 May 2019
If you get an error, give us the full text of the error message. What does the num input command mean?
Note that none of the sprintf call in your code make any sense. The first two lack any formatting parameter to make use of the i, the 3rd one is pointless. Also note that sprintf is not a function that can open a file (for reading or anything else).
When I run it, this is the error message for num, which I understand as to why there's an error:
Undefined function or variable 'num'
Error in Lab3FST (line 8)
for i=1:num
When I enter a number it goes away but I'm not sure what the number is referring to.
The input gives the same error message.
So instead of sprintf, would I use fid=fopen(filename)?
Or instead of fid, I'd use the code you suggested for readtable?
if you're using code you don't understand, it's no surprise you get problem.
In the code you've shown num is meant to be the number of input files. The code is meant to extract column B2:B15039 of each file and sum them all. Except it's full of bugs.
sprintf was the correct function to use. The comment is simply wrong. The line is meant to construct a filename, except that the format was forgotten, so it always construct the name of the first file.
You should certainly not use fopen on an excel file (unless you know how to decode an excel file at the binary level).
Yeah sorry, my professors gave us this code to use and it's been a headache to figure out. Thanks for the help though.

Sign in to comment.

Answers (1)

Read the content of the file with readmatrix (R2019a only), readtable, or xlsread and pass whatever it is you want from that to fft or fft2.
Without more details about what's in the file, that's all that can be said.

2 Comments

The excel files is just time in the x axis (column one) and voltage in the y axis (column 2).
Would the code be similar to this?
[X,TXT,RAW] = xlsread('yourfile.xls');
xdft = fft(X(:,2));
Using xlsread it would probably be:
data = xlsread('yourfile.xls'); %no point in asking for text and raw if you don't use them
xdft = fft(data(:, 2));
Personally, I'd use readtable to make it easier to understand what is what:
data = readtable('yourfile.xls', 'ReadVariableNames', false);
data.Properties.VariableNames = {'time', 'voltage'};
xdft = fft(data.Voltage);

Sign in to comment.

Categories

Asked:

on 17 May 2019

Commented:

on 17 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!