Clear Filters
Clear Filters

how to convert or generate prn files to xlsx files

40 views (last 30 days)
neela
neela on 29 Jul 2024
Edited: dpb on 3 Aug 2024 at 13:29
i have .prn files i want to covert those files to xlsx files
  7 Comments
Walter Roberson
Walter Roberson on 30 Jul 2024
data = readcell('C:\Users\vzmglb\Desktop\29july2024\CITYRUN_LK0_STOICH_1.prn', 'filetype', 'text');
writecell(data,'File1.xlsx')
winopen('File1.xlsx')
I am not completely clear as to whether the username is vzmg1b or vzmglb -- looks more like vzmglb to me.
dpb
dpb on 30 Jul 2024
Edited: dpb on 30 Jul 2024
Yeah, I forget about the limited number of file extensions that Mathworks has identified as text....I figure it ought to try anything that isn't known to be something else, but there are so many arcane naming conventions, I guess it's tough. But, .prn is pretty common...

Sign in to comment.

Answers (1)

dpb
dpb on 30 Jul 2024
Edited: dpb on 30 Jul 2024
More generically, but the same idea
ROOT='C:\Users\vzmglb\Desktop\29july2024'; % set a root for the data files
d=dir(fullfile(ROOT,'*.prn')); % return all .prn files; adapt wildcard to suit
for i=1:numel(d); % iterate over all files found
fqn=fullfile(d.folder,d.name); % build fully-qualified filename
data=readcell(fqn,'filetype','text'); % read the .prn file
fqn=strrep(fqn,'.prn','.xlsx'); % convert the filename to match new format/type
writecell(data,fqn)
end
The above will put a set of files in the same root folder/directory with the base name the same as the original but with the correct .xlsx extension. You could define a different output folder and build the filename similarly as done above for the input if desired.
  1 Comment
dpb
dpb on 1 Aug 2024
Edited: dpb on 3 Aug 2024 at 13:29
You did not run the code given which is
data=readcell(fqn,'filetype','text'); % read the .prn file
Use the result from the call to dir() with the fully-qualified filename; don't hard code in a name; that defeats the whole purpose of generalizing the code.
In particular, you gave only a file name without the location which isn't where you're running the MATLAB code from so the file isn't found.
If you want a particular file set, as noted before, modify the wildcard matching pattern to return those of interest or further generalize/adapt the basic code to pass a particular name or add in a call to uigetfile to let you use a dialog to select files.
ADDENDUM
"...don't hard code in a name..."
And certainly there's no point in hard-coding a specific file name inside a loop; if it worked by adding the fully-qualified name that would simply read the same file however many times the loop ran; hardly a useful thing...

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!