how to convert or generate prn files to xlsx files

40 次查看(过去 30 天)
i have .prn files i want to covert those files to xlsx files
  7 个评论
Walter Roberson
Walter Roberson 2024-7-30
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 2024-7-30
编辑:dpb 2024-7-30
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...

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2024-7-30
编辑:dpb 2024-7-30
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 个评论
dpb
dpb 2024-8-1
编辑:dpb 2024-8-3,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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by